Tuesday, December 25, 2007

Listing directories only

Some times we need to see only directories... and, as usual, there are a lot of ways to do this..

you can write a quick bash script like this:

for file in *; do if [ -d $file ]; then echo $file; fi; done

This will display, in columns, all subdirectories under the directory you are in.

In GNU it is really simple, you just do this
ls -d */

Now to display each entry on a line you can do this:
ls -d */ | less

or to avoid the 'q' to exit less, then u do this:
ls -d */ | cut -f 1 -d /

To get the effect of ls -l for directories only you can do this:
ls -l | egrep ^d

And you can have more fun, by piping to sort or something else
like
ls -d */ | cut -f 1 -d / | sort -r
which sorts things in reverse order

World Have fun :)

No comments: