How can I sort du -h output by size

Posted by Tom Feiner on Server Fault See other posts from Server Fault or by Tom Feiner
Published on 2009-02-25T13:42:42Z Indexed on 2011/07/01 8:24 UTC
Read the original article Hit count: 252

Filed under:
|
|

I need to get a list of human readable du output.

However, du does not have a "sort by size" option, and piping to "sort" doesn't work with the human readable flag.

For example, running:

du | sort -n -r

Outputs a sorted disk usage by size (descending):

du |sort -n -r
65108   .
61508   ./dir3
2056    ./dir4
1032    ./dir1
508     ./dir2

However, running it with the human readable flag, does not sort properly:

du -h | sort -n -r

508K    ./dir2
64M     .
61M     ./dir3
2.1M    ./dir4
1.1M    ./dir1

Does anyone know of a way to sort du -h by size?

© Server Fault or respective owner

Related posts about linux

Related posts about bash