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
1 hour ago
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
42 mins ago
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
39 mins ago
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 40 mins ago
asked 1 hour ago
Sugatur 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
1 hour ago
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
42 mins ago
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
39 mins ago
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
1 hour ago
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
42 mins ago
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
39 mins ago
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
1 hour ago
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
1 hour ago
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
42 mins ago
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
42 mins ago
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
39 mins ago
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
39 mins ago
add a comment |
4 Answers
4
active
oldest
votes
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 |
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
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
New contributor
It would add some extra lines to the end of the output though. Use it with-s
too.
– Kusalananda
1 hour ago
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 extglob
printf '%sn' image_script/**/
The extglob
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 extglob
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
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
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 1 hour ago
nohillside
2,372919
2,372919
add a comment |
add a comment |
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
add a comment |
You can try
find `pwd` -type d
Or replace pwd by the absolute path of your folder
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 1 hour ago
yhu420
63
63
add a comment |
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
New contributor
It would add some extra lines to the end of the output though. Use it with-s
too.
– Kusalananda
1 hour ago
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
New contributor
It would add some extra lines to the end of the output though. Use it with-s
too.
– Kusalananda
1 hour ago
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
New contributor
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
New contributor
New contributor
answered 1 hour ago
finswimmer
1011
1011
New contributor
New contributor
It would add some extra lines to the end of the output though. Use it with-s
too.
– Kusalananda
1 hour ago
add a comment |
It would add some extra lines to the end of the output though. Use it with-s
too.
– Kusalananda
1 hour ago
It would add some extra lines to the end of the output though. Use it with
-s
too.– Kusalananda
1 hour ago
It would add some extra lines to the end of the output though. Use it with
-s
too.– Kusalananda
1 hour ago
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 extglob
printf '%sn' image_script/**/
The extglob
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 extglob
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
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 extglob
printf '%sn' image_script/**/
The extglob
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 extglob
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
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 extglob
printf '%sn' image_script/**/
The extglob
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 extglob
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 extglob
printf '%sn' image_script/**/
The extglob
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 extglob
for pathname in image_script/**/; do
printf '%sn' "${pathname%/}"
done
edited 1 hour ago
answered 1 hour ago
Kusalananda
122k16230375
122k16230375
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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
1 hour ago
sorry it is relative paths , yes I need to execute few commands in each folder
– Sugatur Deekshith S N
42 mins ago
Please amend your question to describe the full problem then. Printing some names may be different from doing something with them.
– nohillside
39 mins ago