List only directories, one per line

ls -l | grep ^d | sed 's:.*\ ::g'
Normally, if you just want to see directories you'd use brianmuckian's command 'ls -d *\', but I ran into problems trying to use that command in my script because there are often multiple directories per line. If you need to script something with directories and want to guarantee that there is only one entry per line, this is the fastest way i know
Sample Output
seth@Laptop:~$ ls -l | grep ^d  | sed 's:.*\ ::g'

By: LinuxMan
2011-08-06 23:52:46

7 Alternatives + Submit Alt

What Others Think

yes, I'm commenting on my own script just minutes after posting it, it seems (quite obvious now) that it will only work when there are no spaces in the directory name. there is no easy fix for that without inflating the sed command, perhaps a find command is more efficient
LinuxMan · 514 weeks and 5 days ago
LinuxMan, have you read 'man ls' for obvious parameter 'ls -1' Also, when 'ls' output is a pipe - it always output in one-column format
Avenger · 514 weeks and 5 days ago
Try 's!.*:.. !!' as your expression instead.
TheMightyBuzzard · 514 weeks and 5 days ago
here is a zsh command for that, using the extended glob (/) ls -1d *(/)
Seebi · 514 weeks and 5 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? 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.


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: