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
















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














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


















  • 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










4 Answers
4






active

oldest

votes


















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














    You can try



    find `pwd` -type d


    Or replace pwd by the absolute path of your folder






    share|improve this answer





























      0














      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








      New contributor




      finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















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





















      0














      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





      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









        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 1 hour ago









            nohillside

            2,372919




            2,372919

























                0














                You can try



                find `pwd` -type d


                Or replace pwd by the absolute path of your folder






                share|improve this answer


























                  0














                  You can try



                  find `pwd` -type d


                  Or replace pwd by the absolute path of your folder






                  share|improve this answer
























                    0












                    0








                    0






                    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 1 hour ago









                    yhu420

                    63




                    63























                        0














                        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








                        New contributor




                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.


















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


















                        0














                        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








                        New contributor




                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.


















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
















                        0












                        0








                        0






                        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








                        New contributor




                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        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








                        New contributor




                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        share|improve this answer



                        share|improve this answer






                        New contributor




                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        answered 1 hour ago









                        finswimmer

                        1011




                        1011




                        New contributor




                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.





                        New contributor





                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






                        finswimmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.












                        • 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






                        It would add some extra lines to the end of the output though. Use it with -s too.
                        – Kusalananda
                        1 hour ago













                        0














                        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





                        share|improve this answer




























                          0














                          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





                          share|improve this answer


























                            0












                            0








                            0






                            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





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






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 1 hour ago

























                            answered 1 hour ago









                            Kusalananda

                            122k16230375




                            122k16230375






























                                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.





                                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.




                                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

                                CARDNET

                                Boot-repair Failure: Unable to locate package grub-common:i386

                                濃尾地震