Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

6 Answers 6

up vote 53 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
1  
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 at 17:25
3  
@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 at 0:20

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

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

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
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
    
+1 correcto mundo –  Shutupsquare Sep 14 '11 at 8:54

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
    
Thanks for that had them wrong way round sorted now +1 –  Shutupsquare Sep 14 '11 at 9:19
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.