Last Updated: February 25, 2016
·
1.151K
· pixeloution

List of directories, sorted by size of contents

Its often a pain to get the syntax just right if you're not someone who does a ton of server admin. This shell command ( ubuntu ) will give you a list of directories, sorted by size, in human-readable size notation.

du --max-depth=1 -k * | sort -nr | cut -f2 | xargs -d '\n' du -sh

Some *nix systems won't support --max-depth but will support -d for the same result, and some won't support then -d option for xargs in which case you can do something a bit more convoluted ( MacOS example )

du -d 1 -k * | sort -nr | cut -f2 | tr '\n' '\0' | xargs -0 du -sh

This version sends the results of cut to tr which replaces the newlines with nulls, and then tells xargs to expect null terminated arguments. The first version - which is a more modern version of xargs - allows you to specify what terminates an argument (so in the first version we explicitly tell it newline).

Happy directory sorting!

2 Responses
Add your response

Beautiful :)

over 1 year ago ·

btw this will work on practically any distro and probably BSD, not Ubuntu specific at all

over 1 year ago ·