Sort directories by number of files contained

Sat, 2009-05-09

Here is a one-liner which sorts all the directories under ‘/’ by the number of files contained:

find / -type d -exec sh -c 'ls -a1 "$1" | wc -l' \"{}\" {} \; -print | \
sed  '$!N;s/\n/ /' | sort -gr

I recently needed this on a linux server where the inode quota was exceeded. Through the one-liner I discovered where most of the inodes where consumed.