Tuesday, June 10, 2008

Quickly Find Subdir

I used to use a long-winded invocation of find with the prune option to non-recursively find all the directories in the current dir:

find . -name . -o -type d -print -prune

From there, I moved to using ls and grep:

ls -l | grep '^d'

I recently learned that the following shell pattern works just as well:

ls -ld */.


egypt said...

shell expansions are also useful for showing all hidden files in the current directory like so:

ls -d .[^.]*

The "-d" prevents listing contents of hidden subdirectories. I have this aliased in my .bashrc as "l." -- and right beneath it now is your tip as "lsd"

Steve Laniel said...

The trouble with using '*', in general, is that it fails when there are too many entries in a directory or when any entries are too large. That's because the shell expands '*', and the shell has a certain length limitation. This is where iterator-type methods (like find(1)) shine.

Anonymous said...

If you're running zsh, you can run

ls -d *(/)

The / in the brackets tells the shell to only expand the directories that match *. You can do really wild things with zsh matching like...

print /etc/**/*(@)

to print all symlinks found under /etc.