How to list folders using bash commands?











up vote
51
down vote

favorite
19












Is there any way to list just the folders in a directory using bash commands? ( as the ls command lists all the files and folders )










share|improve this question




























    up vote
    51
    down vote

    favorite
    19












    Is there any way to list just the folders in a directory using bash commands? ( as the ls command lists all the files and folders )










    share|improve this question


























      up vote
      51
      down vote

      favorite
      19









      up vote
      51
      down vote

      favorite
      19






      19





      Is there any way to list just the folders in a directory using bash commands? ( as the ls command lists all the files and folders )










      share|improve this question















      Is there any way to list just the folders in a directory using bash commands? ( as the ls command lists all the files and folders )







      linux command-line ls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 21 '14 at 17:17









      That Brazilian Guy

      4,73775090




      4,73775090










      asked Sep 14 '11 at 8:27









      SpiXel

      4643714




      4643714






















          8 Answers
          8






          active

          oldest

          votes

















          up vote
          64
          down vote



          accepted










          You can use:



          ls -d -- */


          Since all directories end in /, this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.






          share|improve this answer



















          • 2




            ls -- */ lists all directories with their contents below
            – Vins
            Jan 21 '13 at 11:32












          • @8088 What is the difference between ls -d -- */ and ls -d */?
            – Louis
            Mar 24 '14 at 17:25






          • 4




            @Louis, -- is conventionally used to mark the end of options, so that if a file is named -l ls won't interpret it as the long listing format option.
            – Cristian Ciupitu
            Apr 22 '14 at 0:20


















          up vote
          26
          down vote













          Stephen Martin's response gave a warning, and listed the current folder as well, so I'd suggest



          find . -mindepth 1 -maxdepth 1 -type d


          (This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)






          share|improve this answer

















          • 1




            Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1
            – matchew
            Dec 21 '12 at 16:16




















          up vote
          12
          down vote













          find . -maxdepth 1 -type d


          Will list just folders. And as Teddy pointed out you'll need -maxdepth to stop it recusrsing into sub dirs






          share|improve this answer



















          • 5




            You probably want -maxdepth 1 too.
            – Teddy
            Sep 14 '11 at 8:40


















          up vote
          5
          down vote













          Daniel’s answer is correct. Here are some useful additions, though.



          To avoid listing hidden folders (like .git), try this:



          find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" )


          And to replace the dreaded dot slash at the beginning of find output in some environments, use this:



          find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" ) | sed 's|^./||g'





          share|improve this answer






























            up vote
            1
            down vote













            You're "not supposed to" parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.



            if you don't want ls or find, you may want to try filtering "*" with "[ -d ]".



            I did just that, for some reason ls and find weren't working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of



            for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done





            share|improve this answer




























              up vote
              0
              down vote













              Just to emphasize a thing that confused me out here, in respect to glob patterns selection; say you have this:



              $ cd /tmp
              $ mkdir testglob
              $ for ix in {00,01,02,03} ; do mkdir testglob/mydir_${ix} ; done
              $ for ix in {00,01,02,03} ; do touch testglob/myfile_${ix} ; done
              $ for ix in {00,01,02,03} ; do touch testglob/mydir_${ix}.txt ; done
              $ for ix in {00,01,02,03} ; do mkdir testglob/otherdir_${ix} ; done
              $ tree testglob/
              testglob/
              ├── mydir_00
              ├── mydir_00.txt
              ├── mydir_01
              ├── mydir_01.txt
              ├── mydir_02
              ├── mydir_02.txt
              ├── mydir_03
              ├── mydir_03.txt
              ├── myfile_00
              ├── myfile_01
              ├── myfile_02
              ├── myfile_03
              ├── otherdir_00
              ├── otherdir_01
              ├── otherdir_02
              └── otherdir_03

              8 directories, 8 files


              So, say here you want to select only mydir* directories. Note that if you leave out the terminating slash, ls -d will list files as well:



              $ ls -d testglob/mydir*   # also `ls -d -- testglob/mydir*`
              testglob/mydir_00 testglob/mydir_01 testglob/mydir_02 testglob/mydir_03
              testglob/mydir_00.txt testglob/mydir_01.txt testglob/mydir_02.txt testglob/mydir_03.txt


              ... however, with a terminating slash, then only directories are listed:



              $ ls -d testglob/mydir*/   # also `ls -d -- testglob/mydir*/`
              testglob/mydir_00/ testglob/mydir_01/ testglob/mydir_02/ testglob/mydir_03/





              share|improve this answer




























                up vote
                0
                down vote













                printf "%sn" */ will list all directories in the $PWD.



                echo */ will also work, but in a long one-line, more difficult when names have spaces.






                share|improve this answer




























                  up vote
                  0
                  down vote













                  You can also use:



                  du


                  Or:



                  git ls-tree -d -r --name-only @



                  • http://git-scm.com/docs/git-ls-tree

                  • http://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html






                  share|improve this answer





















                    Your Answer








                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "3"
                    };
                    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',
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    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%2fsuperuser.com%2fquestions%2f335376%2fhow-to-list-folders-using-bash-commands%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    8 Answers
                    8






                    active

                    oldest

                    votes








                    8 Answers
                    8






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    64
                    down vote



                    accepted










                    You can use:



                    ls -d -- */


                    Since all directories end in /, this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.






                    share|improve this answer



















                    • 2




                      ls -- */ lists all directories with their contents below
                      – Vins
                      Jan 21 '13 at 11:32












                    • @8088 What is the difference between ls -d -- */ and ls -d */?
                      – Louis
                      Mar 24 '14 at 17:25






                    • 4




                      @Louis, -- is conventionally used to mark the end of options, so that if a file is named -l ls won't interpret it as the long listing format option.
                      – Cristian Ciupitu
                      Apr 22 '14 at 0:20















                    up vote
                    64
                    down vote



                    accepted










                    You can use:



                    ls -d -- */


                    Since all directories end in /, this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.






                    share|improve this answer



















                    • 2




                      ls -- */ lists all directories with their contents below
                      – Vins
                      Jan 21 '13 at 11:32












                    • @8088 What is the difference between ls -d -- */ and ls -d */?
                      – Louis
                      Mar 24 '14 at 17:25






                    • 4




                      @Louis, -- is conventionally used to mark the end of options, so that if a file is named -l ls won't interpret it as the long listing format option.
                      – Cristian Ciupitu
                      Apr 22 '14 at 0:20













                    up vote
                    64
                    down vote



                    accepted







                    up vote
                    64
                    down vote



                    accepted






                    You can use:



                    ls -d -- */


                    Since all directories end in /, this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.






                    share|improve this answer














                    You can use:



                    ls -d -- */


                    Since all directories end in /, this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 14 '11 at 8:47

























                    answered Sep 14 '11 at 8:35









                    3498DB

                    15.6k114762




                    15.6k114762








                    • 2




                      ls -- */ lists all directories with their contents below
                      – Vins
                      Jan 21 '13 at 11:32












                    • @8088 What is the difference between ls -d -- */ and ls -d */?
                      – Louis
                      Mar 24 '14 at 17:25






                    • 4




                      @Louis, -- is conventionally used to mark the end of options, so that if a file is named -l ls won't interpret it as the long listing format option.
                      – Cristian Ciupitu
                      Apr 22 '14 at 0:20














                    • 2




                      ls -- */ lists all directories with their contents below
                      – Vins
                      Jan 21 '13 at 11:32












                    • @8088 What is the difference between ls -d -- */ and ls -d */?
                      – Louis
                      Mar 24 '14 at 17:25






                    • 4




                      @Louis, -- is conventionally used to mark the end of options, so that if a file is named -l ls won't interpret it as the long listing format option.
                      – Cristian Ciupitu
                      Apr 22 '14 at 0:20








                    2




                    2




                    ls -- */ lists all directories with their contents below
                    – Vins
                    Jan 21 '13 at 11:32






                    ls -- */ lists all directories with their contents below
                    – Vins
                    Jan 21 '13 at 11:32














                    @8088 What is the difference between ls -d -- */ and ls -d */?
                    – Louis
                    Mar 24 '14 at 17:25




                    @8088 What is the difference between ls -d -- */ and ls -d */?
                    – Louis
                    Mar 24 '14 at 17:25




                    4




                    4




                    @Louis, -- is conventionally used to mark the end of options, so that if a file is named -l ls won't interpret it as the long listing format option.
                    – Cristian Ciupitu
                    Apr 22 '14 at 0:20




                    @Louis, -- is conventionally used to mark the end of options, so that if a file is named -l ls won't interpret it as the long listing format option.
                    – Cristian Ciupitu
                    Apr 22 '14 at 0:20












                    up vote
                    26
                    down vote













                    Stephen Martin's response gave a warning, and listed the current folder as well, so I'd suggest



                    find . -mindepth 1 -maxdepth 1 -type d


                    (This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)






                    share|improve this answer

















                    • 1




                      Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1
                      – matchew
                      Dec 21 '12 at 16:16

















                    up vote
                    26
                    down vote













                    Stephen Martin's response gave a warning, and listed the current folder as well, so I'd suggest



                    find . -mindepth 1 -maxdepth 1 -type d


                    (This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)






                    share|improve this answer

















                    • 1




                      Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1
                      – matchew
                      Dec 21 '12 at 16:16















                    up vote
                    26
                    down vote










                    up vote
                    26
                    down vote









                    Stephen Martin's response gave a warning, and listed the current folder as well, so I'd suggest



                    find . -mindepth 1 -maxdepth 1 -type d


                    (This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)






                    share|improve this answer












                    Stephen Martin's response gave a warning, and listed the current folder as well, so I'd suggest



                    find . -mindepth 1 -maxdepth 1 -type d


                    (This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 14 '11 at 9:09









                    daniel kullmann

                    54049




                    54049








                    • 1




                      Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1
                      – matchew
                      Dec 21 '12 at 16:16
















                    • 1




                      Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1
                      – matchew
                      Dec 21 '12 at 16:16










                    1




                    1




                    Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1
                    – matchew
                    Dec 21 '12 at 16:16






                    Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1
                    – matchew
                    Dec 21 '12 at 16:16












                    up vote
                    12
                    down vote













                    find . -maxdepth 1 -type d


                    Will list just folders. And as Teddy pointed out you'll need -maxdepth to stop it recusrsing into sub dirs






                    share|improve this answer



















                    • 5




                      You probably want -maxdepth 1 too.
                      – Teddy
                      Sep 14 '11 at 8:40















                    up vote
                    12
                    down vote













                    find . -maxdepth 1 -type d


                    Will list just folders. And as Teddy pointed out you'll need -maxdepth to stop it recusrsing into sub dirs






                    share|improve this answer



















                    • 5




                      You probably want -maxdepth 1 too.
                      – Teddy
                      Sep 14 '11 at 8:40













                    up vote
                    12
                    down vote










                    up vote
                    12
                    down vote









                    find . -maxdepth 1 -type d


                    Will list just folders. And as Teddy pointed out you'll need -maxdepth to stop it recusrsing into sub dirs






                    share|improve this answer














                    find . -maxdepth 1 -type d


                    Will list just folders. And as Teddy pointed out you'll need -maxdepth to stop it recusrsing into sub dirs







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 14 '11 at 9:17

























                    answered Sep 14 '11 at 8:33









                    0x7c0

                    1,6601323




                    1,6601323








                    • 5




                      You probably want -maxdepth 1 too.
                      – Teddy
                      Sep 14 '11 at 8:40














                    • 5




                      You probably want -maxdepth 1 too.
                      – Teddy
                      Sep 14 '11 at 8:40








                    5




                    5




                    You probably want -maxdepth 1 too.
                    – Teddy
                    Sep 14 '11 at 8:40




                    You probably want -maxdepth 1 too.
                    – Teddy
                    Sep 14 '11 at 8:40










                    up vote
                    5
                    down vote













                    Daniel’s answer is correct. Here are some useful additions, though.



                    To avoid listing hidden folders (like .git), try this:



                    find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" )


                    And to replace the dreaded dot slash at the beginning of find output in some environments, use this:



                    find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" ) | sed 's|^./||g'





                    share|improve this answer



























                      up vote
                      5
                      down vote













                      Daniel’s answer is correct. Here are some useful additions, though.



                      To avoid listing hidden folders (like .git), try this:



                      find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" )


                      And to replace the dreaded dot slash at the beginning of find output in some environments, use this:



                      find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" ) | sed 's|^./||g'





                      share|improve this answer

























                        up vote
                        5
                        down vote










                        up vote
                        5
                        down vote









                        Daniel’s answer is correct. Here are some useful additions, though.



                        To avoid listing hidden folders (like .git), try this:



                        find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" )


                        And to replace the dreaded dot slash at the beginning of find output in some environments, use this:



                        find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" ) | sed 's|^./||g'





                        share|improve this answer














                        Daniel’s answer is correct. Here are some useful additions, though.



                        To avoid listing hidden folders (like .git), try this:



                        find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" )


                        And to replace the dreaded dot slash at the beginning of find output in some environments, use this:



                        find . -mindepth 1 -maxdepth 1 -type d  ( ! -iname ".*" ) | sed 's|^./||g'






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Mar 20 '17 at 10:17









                        Community

                        1




                        1










                        answered May 9 '13 at 7:53









                        Mathias Bynens

                        1,47152337




                        1,47152337






















                            up vote
                            1
                            down vote













                            You're "not supposed to" parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.



                            if you don't want ls or find, you may want to try filtering "*" with "[ -d ]".



                            I did just that, for some reason ls and find weren't working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of



                            for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done





                            share|improve this answer

























                              up vote
                              1
                              down vote













                              You're "not supposed to" parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.



                              if you don't want ls or find, you may want to try filtering "*" with "[ -d ]".



                              I did just that, for some reason ls and find weren't working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of



                              for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done





                              share|improve this answer























                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                You're "not supposed to" parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.



                                if you don't want ls or find, you may want to try filtering "*" with "[ -d ]".



                                I did just that, for some reason ls and find weren't working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of



                                for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done





                                share|improve this answer












                                You're "not supposed to" parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.



                                if you don't want ls or find, you may want to try filtering "*" with "[ -d ]".



                                I did just that, for some reason ls and find weren't working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of



                                for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 31 '14 at 0:32









                                the lone debianer

                                163




                                163






















                                    up vote
                                    0
                                    down vote













                                    Just to emphasize a thing that confused me out here, in respect to glob patterns selection; say you have this:



                                    $ cd /tmp
                                    $ mkdir testglob
                                    $ for ix in {00,01,02,03} ; do mkdir testglob/mydir_${ix} ; done
                                    $ for ix in {00,01,02,03} ; do touch testglob/myfile_${ix} ; done
                                    $ for ix in {00,01,02,03} ; do touch testglob/mydir_${ix}.txt ; done
                                    $ for ix in {00,01,02,03} ; do mkdir testglob/otherdir_${ix} ; done
                                    $ tree testglob/
                                    testglob/
                                    ├── mydir_00
                                    ├── mydir_00.txt
                                    ├── mydir_01
                                    ├── mydir_01.txt
                                    ├── mydir_02
                                    ├── mydir_02.txt
                                    ├── mydir_03
                                    ├── mydir_03.txt
                                    ├── myfile_00
                                    ├── myfile_01
                                    ├── myfile_02
                                    ├── myfile_03
                                    ├── otherdir_00
                                    ├── otherdir_01
                                    ├── otherdir_02
                                    └── otherdir_03

                                    8 directories, 8 files


                                    So, say here you want to select only mydir* directories. Note that if you leave out the terminating slash, ls -d will list files as well:



                                    $ ls -d testglob/mydir*   # also `ls -d -- testglob/mydir*`
                                    testglob/mydir_00 testglob/mydir_01 testglob/mydir_02 testglob/mydir_03
                                    testglob/mydir_00.txt testglob/mydir_01.txt testglob/mydir_02.txt testglob/mydir_03.txt


                                    ... however, with a terminating slash, then only directories are listed:



                                    $ ls -d testglob/mydir*/   # also `ls -d -- testglob/mydir*/`
                                    testglob/mydir_00/ testglob/mydir_01/ testglob/mydir_02/ testglob/mydir_03/





                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote













                                      Just to emphasize a thing that confused me out here, in respect to glob patterns selection; say you have this:



                                      $ cd /tmp
                                      $ mkdir testglob
                                      $ for ix in {00,01,02,03} ; do mkdir testglob/mydir_${ix} ; done
                                      $ for ix in {00,01,02,03} ; do touch testglob/myfile_${ix} ; done
                                      $ for ix in {00,01,02,03} ; do touch testglob/mydir_${ix}.txt ; done
                                      $ for ix in {00,01,02,03} ; do mkdir testglob/otherdir_${ix} ; done
                                      $ tree testglob/
                                      testglob/
                                      ├── mydir_00
                                      ├── mydir_00.txt
                                      ├── mydir_01
                                      ├── mydir_01.txt
                                      ├── mydir_02
                                      ├── mydir_02.txt
                                      ├── mydir_03
                                      ├── mydir_03.txt
                                      ├── myfile_00
                                      ├── myfile_01
                                      ├── myfile_02
                                      ├── myfile_03
                                      ├── otherdir_00
                                      ├── otherdir_01
                                      ├── otherdir_02
                                      └── otherdir_03

                                      8 directories, 8 files


                                      So, say here you want to select only mydir* directories. Note that if you leave out the terminating slash, ls -d will list files as well:



                                      $ ls -d testglob/mydir*   # also `ls -d -- testglob/mydir*`
                                      testglob/mydir_00 testglob/mydir_01 testglob/mydir_02 testglob/mydir_03
                                      testglob/mydir_00.txt testglob/mydir_01.txt testglob/mydir_02.txt testglob/mydir_03.txt


                                      ... however, with a terminating slash, then only directories are listed:



                                      $ ls -d testglob/mydir*/   # also `ls -d -- testglob/mydir*/`
                                      testglob/mydir_00/ testglob/mydir_01/ testglob/mydir_02/ testglob/mydir_03/





                                      share|improve this answer























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        Just to emphasize a thing that confused me out here, in respect to glob patterns selection; say you have this:



                                        $ cd /tmp
                                        $ mkdir testglob
                                        $ for ix in {00,01,02,03} ; do mkdir testglob/mydir_${ix} ; done
                                        $ for ix in {00,01,02,03} ; do touch testglob/myfile_${ix} ; done
                                        $ for ix in {00,01,02,03} ; do touch testglob/mydir_${ix}.txt ; done
                                        $ for ix in {00,01,02,03} ; do mkdir testglob/otherdir_${ix} ; done
                                        $ tree testglob/
                                        testglob/
                                        ├── mydir_00
                                        ├── mydir_00.txt
                                        ├── mydir_01
                                        ├── mydir_01.txt
                                        ├── mydir_02
                                        ├── mydir_02.txt
                                        ├── mydir_03
                                        ├── mydir_03.txt
                                        ├── myfile_00
                                        ├── myfile_01
                                        ├── myfile_02
                                        ├── myfile_03
                                        ├── otherdir_00
                                        ├── otherdir_01
                                        ├── otherdir_02
                                        └── otherdir_03

                                        8 directories, 8 files


                                        So, say here you want to select only mydir* directories. Note that if you leave out the terminating slash, ls -d will list files as well:



                                        $ ls -d testglob/mydir*   # also `ls -d -- testglob/mydir*`
                                        testglob/mydir_00 testglob/mydir_01 testglob/mydir_02 testglob/mydir_03
                                        testglob/mydir_00.txt testglob/mydir_01.txt testglob/mydir_02.txt testglob/mydir_03.txt


                                        ... however, with a terminating slash, then only directories are listed:



                                        $ ls -d testglob/mydir*/   # also `ls -d -- testglob/mydir*/`
                                        testglob/mydir_00/ testglob/mydir_01/ testglob/mydir_02/ testglob/mydir_03/





                                        share|improve this answer












                                        Just to emphasize a thing that confused me out here, in respect to glob patterns selection; say you have this:



                                        $ cd /tmp
                                        $ mkdir testglob
                                        $ for ix in {00,01,02,03} ; do mkdir testglob/mydir_${ix} ; done
                                        $ for ix in {00,01,02,03} ; do touch testglob/myfile_${ix} ; done
                                        $ for ix in {00,01,02,03} ; do touch testglob/mydir_${ix}.txt ; done
                                        $ for ix in {00,01,02,03} ; do mkdir testglob/otherdir_${ix} ; done
                                        $ tree testglob/
                                        testglob/
                                        ├── mydir_00
                                        ├── mydir_00.txt
                                        ├── mydir_01
                                        ├── mydir_01.txt
                                        ├── mydir_02
                                        ├── mydir_02.txt
                                        ├── mydir_03
                                        ├── mydir_03.txt
                                        ├── myfile_00
                                        ├── myfile_01
                                        ├── myfile_02
                                        ├── myfile_03
                                        ├── otherdir_00
                                        ├── otherdir_01
                                        ├── otherdir_02
                                        └── otherdir_03

                                        8 directories, 8 files


                                        So, say here you want to select only mydir* directories. Note that if you leave out the terminating slash, ls -d will list files as well:



                                        $ ls -d testglob/mydir*   # also `ls -d -- testglob/mydir*`
                                        testglob/mydir_00 testglob/mydir_01 testglob/mydir_02 testglob/mydir_03
                                        testglob/mydir_00.txt testglob/mydir_01.txt testglob/mydir_02.txt testglob/mydir_03.txt


                                        ... however, with a terminating slash, then only directories are listed:



                                        $ ls -d testglob/mydir*/   # also `ls -d -- testglob/mydir*/`
                                        testglob/mydir_00/ testglob/mydir_01/ testglob/mydir_02/ testglob/mydir_03/






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Sep 12 '13 at 10:58









                                        sdaau

                                        2,39543657




                                        2,39543657






















                                            up vote
                                            0
                                            down vote













                                            printf "%sn" */ will list all directories in the $PWD.



                                            echo */ will also work, but in a long one-line, more difficult when names have spaces.






                                            share|improve this answer

























                                              up vote
                                              0
                                              down vote













                                              printf "%sn" */ will list all directories in the $PWD.



                                              echo */ will also work, but in a long one-line, more difficult when names have spaces.






                                              share|improve this answer























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                printf "%sn" */ will list all directories in the $PWD.



                                                echo */ will also work, but in a long one-line, more difficult when names have spaces.






                                                share|improve this answer












                                                printf "%sn" */ will list all directories in the $PWD.



                                                echo */ will also work, but in a long one-line, more difficult when names have spaces.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jul 24 '15 at 6:26







                                                user353322





























                                                    up vote
                                                    0
                                                    down vote













                                                    You can also use:



                                                    du


                                                    Or:



                                                    git ls-tree -d -r --name-only @



                                                    • http://git-scm.com/docs/git-ls-tree

                                                    • http://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html






                                                    share|improve this answer

























                                                      up vote
                                                      0
                                                      down vote













                                                      You can also use:



                                                      du


                                                      Or:



                                                      git ls-tree -d -r --name-only @



                                                      • http://git-scm.com/docs/git-ls-tree

                                                      • http://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html






                                                      share|improve this answer























                                                        up vote
                                                        0
                                                        down vote










                                                        up vote
                                                        0
                                                        down vote









                                                        You can also use:



                                                        du


                                                        Or:



                                                        git ls-tree -d -r --name-only @



                                                        • http://git-scm.com/docs/git-ls-tree

                                                        • http://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html






                                                        share|improve this answer












                                                        You can also use:



                                                        du


                                                        Or:



                                                        git ls-tree -d -r --name-only @



                                                        • http://git-scm.com/docs/git-ls-tree

                                                        • http://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Dec 3 at 18:09









                                                        Steven Penny

                                                        4,1101683133




                                                        4,1101683133






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Super User!


                                                            • 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%2fsuperuser.com%2fquestions%2f335376%2fhow-to-list-folders-using-bash-commands%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

                                                            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                                            Mangá

                                                             ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕