Looping through all sub directories in a folder?
I have requirement where in I need print relative path of each folder.
Folder structure is

is there any way by using single for loop I can print absolute path.
output :-
image_script
image_script/artifactory
image_script/artifactory/charts
image_script/artifactory/charts/postgressql
image_script/artifactory/charts/postgressql/templates
image_script/artifactory/templates
Thanks in advance !!!!
shell-script shell for
add a comment |
I have requirement where in I need print relative path of each folder.
Folder structure is

is there any way by using single for loop I can print absolute path.
output :-
image_script
image_script/artifactory
image_script/artifactory/charts
image_script/artifactory/charts/postgressql
image_script/artifactory/charts/postgressql/templates
image_script/artifactory/templates
Thanks in advance !!!!
shell-script shell for
Your sample output shows relative paths, not absolute ones. Also, do you just want to list them in your terminal or will the paths be the input for something else?
– nohillside
Jan 4 at 10:59
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
Jan 4 at 11:53
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
Jan 4 at 11:56
add a comment |
I have requirement where in I need print relative path of each folder.
Folder structure is

is there any way by using single for loop I can print absolute path.
output :-
image_script
image_script/artifactory
image_script/artifactory/charts
image_script/artifactory/charts/postgressql
image_script/artifactory/charts/postgressql/templates
image_script/artifactory/templates
Thanks in advance !!!!
shell-script shell for
I have requirement where in I need print relative path of each folder.
Folder structure is

is there any way by using single for loop I can print absolute path.
output :-
image_script
image_script/artifactory
image_script/artifactory/charts
image_script/artifactory/charts/postgressql
image_script/artifactory/charts/postgressql/templates
image_script/artifactory/templates
Thanks in advance !!!!
shell-script shell for
shell-script shell for
edited Jan 4 at 11:56
Sugatur Deekshith S N
asked Jan 4 at 10:55
Sugatur Deekshith S NSugatur Deekshith S N
245
245
Your sample output shows relative paths, not absolute ones. Also, do you just want to list them in your terminal or will the paths be the input for something else?
– nohillside
Jan 4 at 10:59
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
Jan 4 at 11:53
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
Jan 4 at 11:56
add a comment |
Your sample output shows relative paths, not absolute ones. Also, do you just want to list them in your terminal or will the paths be the input for something else?
– nohillside
Jan 4 at 10:59
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
Jan 4 at 11:53
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
Jan 4 at 11:56
Your sample output shows relative paths, not absolute ones. Also, do you just want to list them in your terminal or will the paths be the input for something else?
– nohillside
Jan 4 at 10:59
Your sample output shows relative paths, not absolute ones. Also, do you just want to list them in your terminal or will the paths be the input for something else?
– nohillside
Jan 4 at 10:59
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
Jan 4 at 11:53
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
Jan 4 at 11:53
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
Jan 4 at 11:56
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
Jan 4 at 11:56
add a comment |
4 Answers
4
active
oldest
votes
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
1
With bash, you can use"$PWD"instead of callingpwd. Also, it's preferable to use$(...)instead of backticks
– glenn jackman
Jan 4 at 15:42
add a comment |
To get the pathnames of all directories from the top-most image_script directory:
find image_script -type d
This will include the image_script directory itself.
To get absolute pathnames, i.e. pathnames that start with a /, specify the full path to the image_script directory on the find command line.
Use find images_script -depth -type d to get the pathnames in a depth-first ordering.
With bash:
shopt -s globstar
printf '%sn' image_script/**/
The globstar shell option enables the use of ** to match across / in pathnames. This would output all directory pathnames with a trailing / though. To avoid that:
shopt -s globstar
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
**matching is turned on withglobstarinstead ofextglob, right?
– Haxiel
Jan 4 at 13:27
@Haxiel That's absolutely true! Thanks! I don't know what I was thinking...
– Kusalananda
Jan 4 at 15:14
add a comment |
You can use tree for it.
$ tree -d -f -i /path/to/root_folder
-d prints only directory.
-f prepend the full path.
-i is used for not printing indentation lines.
fin swimmer
It would add some extra lines to the end of the output though. Use it with-stoo.
– Kusalananda
Jan 4 at 11:12
add a comment |
Using a for loop might not be the best option here, better use find:
cd /PATH/TO/image_script/..
find image_script -type d
If you need the absolute path you can use
find /PATH/TO/image_script -type d
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492447%2flooping-through-all-sub-directories-in-a-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
1
With bash, you can use"$PWD"instead of callingpwd. Also, it's preferable to use$(...)instead of backticks
– glenn jackman
Jan 4 at 15:42
add a comment |
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
1
With bash, you can use"$PWD"instead of callingpwd. Also, it's preferable to use$(...)instead of backticks
– glenn jackman
Jan 4 at 15:42
add a comment |
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
answered Jan 4 at 11:04
yhu420yhu420
335
335
1
With bash, you can use"$PWD"instead of callingpwd. Also, it's preferable to use$(...)instead of backticks
– glenn jackman
Jan 4 at 15:42
add a comment |
1
With bash, you can use"$PWD"instead of callingpwd. Also, it's preferable to use$(...)instead of backticks
– glenn jackman
Jan 4 at 15:42
1
1
With bash, you can use
"$PWD" instead of calling pwd. Also, it's preferable to use $(...) instead of backticks– glenn jackman
Jan 4 at 15:42
With bash, you can use
"$PWD" instead of calling pwd. Also, it's preferable to use $(...) instead of backticks– glenn jackman
Jan 4 at 15:42
add a comment |
To get the pathnames of all directories from the top-most image_script directory:
find image_script -type d
This will include the image_script directory itself.
To get absolute pathnames, i.e. pathnames that start with a /, specify the full path to the image_script directory on the find command line.
Use find images_script -depth -type d to get the pathnames in a depth-first ordering.
With bash:
shopt -s globstar
printf '%sn' image_script/**/
The globstar shell option enables the use of ** to match across / in pathnames. This would output all directory pathnames with a trailing / though. To avoid that:
shopt -s globstar
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
**matching is turned on withglobstarinstead ofextglob, right?
– Haxiel
Jan 4 at 13:27
@Haxiel That's absolutely true! Thanks! I don't know what I was thinking...
– Kusalananda
Jan 4 at 15:14
add a comment |
To get the pathnames of all directories from the top-most image_script directory:
find image_script -type d
This will include the image_script directory itself.
To get absolute pathnames, i.e. pathnames that start with a /, specify the full path to the image_script directory on the find command line.
Use find images_script -depth -type d to get the pathnames in a depth-first ordering.
With bash:
shopt -s globstar
printf '%sn' image_script/**/
The globstar shell option enables the use of ** to match across / in pathnames. This would output all directory pathnames with a trailing / though. To avoid that:
shopt -s globstar
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
**matching is turned on withglobstarinstead ofextglob, right?
– Haxiel
Jan 4 at 13:27
@Haxiel That's absolutely true! Thanks! I don't know what I was thinking...
– Kusalananda
Jan 4 at 15:14
add a comment |
To get the pathnames of all directories from the top-most image_script directory:
find image_script -type d
This will include the image_script directory itself.
To get absolute pathnames, i.e. pathnames that start with a /, specify the full path to the image_script directory on the find command line.
Use find images_script -depth -type d to get the pathnames in a depth-first ordering.
With bash:
shopt -s globstar
printf '%sn' image_script/**/
The globstar shell option enables the use of ** to match across / in pathnames. This would output all directory pathnames with a trailing / though. To avoid that:
shopt -s globstar
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
To get the pathnames of all directories from the top-most image_script directory:
find image_script -type d
This will include the image_script directory itself.
To get absolute pathnames, i.e. pathnames that start with a /, specify the full path to the image_script directory on the find command line.
Use find images_script -depth -type d to get the pathnames in a depth-first ordering.
With bash:
shopt -s globstar
printf '%sn' image_script/**/
The globstar shell option enables the use of ** to match across / in pathnames. This would output all directory pathnames with a trailing / though. To avoid that:
shopt -s globstar
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
edited Jan 4 at 15:15
answered Jan 4 at 11:04
KusalanandaKusalananda
124k16233384
124k16233384
**matching is turned on withglobstarinstead ofextglob, right?
– Haxiel
Jan 4 at 13:27
@Haxiel That's absolutely true! Thanks! I don't know what I was thinking...
– Kusalananda
Jan 4 at 15:14
add a comment |
**matching is turned on withglobstarinstead ofextglob, right?
– Haxiel
Jan 4 at 13:27
@Haxiel That's absolutely true! Thanks! I don't know what I was thinking...
– Kusalananda
Jan 4 at 15:14
** matching is turned on with globstar instead of extglob, right?– Haxiel
Jan 4 at 13:27
** matching is turned on with globstar instead of extglob, right?– Haxiel
Jan 4 at 13:27
@Haxiel That's absolutely true! Thanks! I don't know what I was thinking...
– Kusalananda
Jan 4 at 15:14
@Haxiel That's absolutely true! Thanks! I don't know what I was thinking...
– Kusalananda
Jan 4 at 15:14
add a comment |
You can use tree for it.
$ tree -d -f -i /path/to/root_folder
-d prints only directory.
-f prepend the full path.
-i is used for not printing indentation lines.
fin swimmer
It would add some extra lines to the end of the output though. Use it with-stoo.
– Kusalananda
Jan 4 at 11:12
add a comment |
You can use tree for it.
$ tree -d -f -i /path/to/root_folder
-d prints only directory.
-f prepend the full path.
-i is used for not printing indentation lines.
fin swimmer
It would add some extra lines to the end of the output though. Use it with-stoo.
– Kusalananda
Jan 4 at 11:12
add a comment |
You can use tree for it.
$ tree -d -f -i /path/to/root_folder
-d prints only directory.
-f prepend the full path.
-i is used for not printing indentation lines.
fin swimmer
You can use tree for it.
$ tree -d -f -i /path/to/root_folder
-d prints only directory.
-f prepend the full path.
-i is used for not printing indentation lines.
fin swimmer
answered Jan 4 at 11:08
finswimmerfinswimmer
1112
1112
It would add some extra lines to the end of the output though. Use it with-stoo.
– Kusalananda
Jan 4 at 11:12
add a comment |
It would add some extra lines to the end of the output though. Use it with-stoo.
– Kusalananda
Jan 4 at 11:12
It would add some extra lines to the end of the output though. Use it with
-s too.– Kusalananda
Jan 4 at 11:12
It would add some extra lines to the end of the output though. Use it with
-s too.– Kusalananda
Jan 4 at 11:12
add a comment |
Using a for loop might not be the best option here, better use find:
cd /PATH/TO/image_script/..
find image_script -type d
If you need the absolute path you can use
find /PATH/TO/image_script -type d
add a comment |
Using a for loop might not be the best option here, better use find:
cd /PATH/TO/image_script/..
find image_script -type d
If you need the absolute path you can use
find /PATH/TO/image_script -type d
add a comment |
Using a for loop might not be the best option here, better use find:
cd /PATH/TO/image_script/..
find image_script -type d
If you need the absolute path you can use
find /PATH/TO/image_script -type d
Using a for loop might not be the best option here, better use find:
cd /PATH/TO/image_script/..
find image_script -type d
If you need the absolute path you can use
find /PATH/TO/image_script -type d
answered Jan 4 at 11:04
nohillsidenohillside
2,382919
2,382919
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492447%2flooping-through-all-sub-directories-in-a-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your sample output shows relative paths, not absolute ones. Also, do you just want to list them in your terminal or will the paths be the input for something else?
– nohillside
Jan 4 at 10:59
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
Jan 4 at 11:53
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
Jan 4 at 11:56