sort the output of the 'du' command by largest first, using human readable output.

du -h --max-depth=1 |sort -rh
In this case I'm just grabbing the next level of subdirectories (and same level regular files) with the --max-depth=1 flag. leaving out that flag will just give you finer resolution. Note that you have to use the -h switch with both 'du' and with 'sort.'
Sample Output
$ du -h --max-depth=1 |sort -rh
4.7M	.
4.3M	./locales
120K	./initializers
112K	./.svn
112K	./environments

By: jambino
2011-11-15 20:30:00

What Others Think

Doesn't seem to work on a linux systems, atleast not on rhel
pykler · 514 weeks ago
Doesn't seem to work on a linux systems, atleast not on rhel
pykler · 514 weeks ago
The -h option to sort is a new one. RHEL 5 does not have that version of sort. You can grab the sort command from a more recent Linux distro and use. I have done that.
DaveQB · 514 weeks ago
Short form of this command would be: du -sh * | sort -rh
sirex · 514 weeks ago
For older versions of sort this should to the trick: du --max-depth=1 | sort -nr | awk '{suffs="k M G"; N=split(suffs,suff," "); for(n=1; $1>1024 && n<N; n=n+1) {$1=$1/1024} printf("%0.1f%s\t%s\n", $1, suff[n], $2)}'
aikikode · 513 weeks and 6 days ago
Yeah, I'm finding that EL 5 (RHEL, CentOS, SL, RedHawk) has an earlier version of sort that doesn't have the '-h' switch. I'm going through some of my older boxes and upgrading sort just to have this. I use it too much not to have it available.
jambino · 513 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: