List only the directories

ls -l | egrep ^d
This command would be useful when it is desirable to list only the directories. 'egrep' chooses only the lines that begin with 'd'.
Sample Output
$ ls -l 
drwxrwx--- 2 sim sim 4096 2009-02-08 14:19 adir
-rwxrwx--- 1 sim sim   52 2009-02-05 13:11 a.sh
-rw-rw---- 1 sim sim    0 2009-02-05 13:09 x.txt
-rw-rw---- 1 sim sim    0 2009-02-05 13:09 y.txt
-rw-rw---- 1 sim sim    0 2009-02-05 13:09 z.txt

$ ls -l | egrep ^d
drwxrwx--- 2 sim sim 4096 2009-02-08 14:19 adir

3
By: ergut
2009-02-08 22:21:55

7 Alternatives + Submit Alt

What Others Think

In zsh, you ca add the modifier (/) to a glob pattern to search only for directories, e.g. ls -ld *(/) lists only directories and ls -ld .*(/) lists only hidden directories (useful in $HOME)
kba · 489 weeks ago
or ls -ld */
wwest4 · 489 weeks ago
or ls -F | grep / since -F adds a trailing slash to directory names. With this approach you avoid all the extra ls -l info.
dmmst19 · 489 weeks ago
Thanks. I like 'ls -ld */' better. Actually looks like you can use 'ls -d */' to avoid the extra information from 'ls -l'.
ergut · 488 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: