Tuesday, October 29, 2013

Sort: Human Numeric

The sort command can accept input data formatted in human readable form. The du -h command is a longtime favorite for finding file sizes in a format that's easy to read with the naked eye. Coupled with sort, you can easily find and display the largest files in a directory tree.
$ du -h | sort -r --human-numeric | head
Update: I should have mentioned that --human-numeric is a recent addition to GNU coreutils, so this won't work on OS X or older distros. Here's a more generic solution (borrowed from here).
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11

6 comments:

Amjith Ramanujam said...

Is there an equivalent of the --human-numeric for OS X?

Amber said...

Oooh. Nice. Even has bash-completion so it's not unwieldy.

Great

Amber said...

Oooh. Nice. Even has bash-completion so it's not unwieldy.

Great

Travis Whitton said...

@Amijith

It's GNU specific. I've updated the post with a generic solution.

Amjith Ramanujam said...

Perfect! Thank you!

Paul Reinerfelt said...

If you have homebrew (or fink or similar) on your OS X, you can install the coreutils package to get an up to date GNU CoreUtils. Just be aware that it will probably be called 'gsort' to avoid conflict with the system version.