Looping through all sub directories in a folder?












0















I have requirement where in I need print relative path of each folder.



Folder structure is



enter image description here



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 !!!!










share|improve this question

























  • 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
















0















I have requirement where in I need print relative path of each folder.



Folder structure is



enter image description here



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 !!!!










share|improve this question

























  • 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














0












0








0








I have requirement where in I need print relative path of each folder.



Folder structure is



enter image description here



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 !!!!










share|improve this question
















I have requirement where in I need print relative path of each folder.



Folder structure is



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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










4 Answers
4






active

oldest

votes


















2














You can try



find `pwd` -type d


Or replace pwd by the absolute path of your folder






share|improve this answer



















  • 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



















2














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





share|improve this answer


























  • ** 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



















1














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






share|improve this answer
























  • It would add some extra lines to the end of the output though. Use it with -s too.

    – Kusalananda
    Jan 4 at 11:12





















0














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





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    You can try



    find `pwd` -type d


    Or replace pwd by the absolute path of your folder






    share|improve this answer



















    • 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
















    2














    You can try



    find `pwd` -type d


    Or replace pwd by the absolute path of your folder






    share|improve this answer



















    • 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














    2












    2








    2







    You can try



    find `pwd` -type d


    Or replace pwd by the absolute path of your folder






    share|improve this answer













    You can try



    find `pwd` -type d


    Or replace pwd by the absolute path of your folder







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 4 at 11:04









    yhu420yhu420

    335




    335








    • 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














    • 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








    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













    2














    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





    share|improve this answer


























    • ** 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
















    2














    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





    share|improve this answer


























    • ** 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














    2












    2








    2







    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





    share|improve this answer















    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 4 at 15:15

























    answered Jan 4 at 11:04









    KusalanandaKusalananda

    124k16233384




    124k16233384













    • ** 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



















    • ** 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

















    ** 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











    1














    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






    share|improve this answer
























    • It would add some extra lines to the end of the output though. Use it with -s too.

      – Kusalananda
      Jan 4 at 11:12


















    1














    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






    share|improve this answer
























    • It would add some extra lines to the end of the output though. Use it with -s too.

      – Kusalananda
      Jan 4 at 11:12
















    1












    1








    1







    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






    share|improve this answer













    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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 -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



















    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













    0














    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





    share|improve this answer




























      0














      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





      share|improve this answer


























        0












        0








        0







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 11:04









        nohillsidenohillside

        2,382919




        2,382919






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            List directoties down one level, excluding some named directories and files

            list processes belonging to a network namespace

            list systemd RuntimeDirectory mounts