Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
7 August 2006 @ 9am

Tagged
Linux, Open Source

Discuss

Comand Line Tip: How to List Only Directories

Finding this took over three tries with Google, which means it took too long and that I’ll forget as soon as I need to do it again.

To list only directories in the current directory, this does the trick:

ls -d */

Though it doesn’t make since to me, because the -d option for ls says:

-d Directories are listed as plain files (not searched recursively).

And “ls -d” alone doesn’t do anything for me…but whatever.

There are some other ways discussed here at the Ubuntu log.


1 Comment

Posted by
Jason
8 August 2006 @ 7am

Rememer that the * is actually handled by the shell. First the shell will expand */ to all directories in the current directory. So say your directory contains:

foo/: directory
bar : regular file
baz/: directory

then “*/” expands to “foo/ baz/”. If you pass this directly to ls (without the -d) it will list the contents of the two directories:

$ ls */
foo:
(contents of foo)

baz:
(contents of baz)

That’s why you need the -d: to tell ls to list directories as themselves and not their contents.


Leave a Comment

Ruby Conf 2006 registration open! Hacking green bar color output into Autotest