Directory Size Lister/Sorter

Have you ever wondered which directories and trees take up all the diskspace? Questions like this come during pruning/archiving, backup, and numerous other activities. Make the following oneliner script (I call it dirsizes) to find out:

du -sm $(find $1 -type d -maxdepth 1 -xdev) | sort -g

The preceding shellscript prints every directory below the one called as an argument, together with its size, sorted with the largest at the bottom. We sort largest at bottom so there's no necessity to pipe it toless. Instead, you can see the largest 24 on the screen after the command.

If you find a large tree, but can't delete the whole thing, you can explore just that tree by using its directory as the argument, and you'll see all its subtrees and how much space they take.

But let's say you want to see ALL directories in the tree, instantly zeroing in on big diskspace directories. Make the following shell script, which I call alldirsizes:

find $1 -type d | xargs du -sm | sort -g

Both these scripts do more than just add filesizes. They take into account inodes, so they reveal the space that would be recovered if the directories were deleted.

Both of these scripts are most accurate when run as root, but they're pretty informative run as just a normal user.

Last updated: 10/02/2005