Find the directories containing the most files

I needed to find the directories containing the most files, so I whipped up this bit of voodoo at the command line:

find . -type f -print | perl -na -F/ -e 'while (pop @F && @F) { print join ("/", @F)."n"; }' | sort | uniq -c | sort -n

This will count the number of files in each directory beneath your current directory, and print out those directories in ascending order by the number of files they contain. It will also print totals for parent directories–if “foo/bar” has one file, “foo/baz” has two, and “foo/” itself has one, then the line for “foo” will be printed with the number four.

Handy for me, possibly handy for you as well. This works in Linux and Mac OS X Leopard.