How to sort it right with du -h | sort ????


Hi all.

i wanna sort a directory after Gb/Mb/kb. When doing a du -d1 -h it show upp by Gb and Mbs. Then i wanna sort it by size, Gb then Mb. I cant make sort to sort it right, anyone knows how?

My line right now is: du -d1 -h | sort -n

But it sorts by number and not size.

Maybe i should do a: du -d1 | sort -n | ?????

But i dont know what the third pipe would be..





sort -n only looks at the numeric value, if you use du with the -h option the G, M and k screw up the results, i.e. 350 kB is more than 5 GB because 350 > 5. Drop the -h and use sort -nr the r flag means reverse, i.e. from biggest to smallest.

Try du -d1 | sort -nr


Thx for replying. Kinda figured that. The problem is that i have to "humanize" the output so anyone can understand the numbers...


How about making it into a CSV file that can be opened via a spreadsheet, and then formatted from there? It may still not be what you're looking for, but here's an example:

du -d1 Library/ | sort -n | awk '{printf "\"%-s\",\"%d\"\n", $2, $1}' > sizes.csv

Now cat the CSV file, which has the doublequotes around the values in addition to the commas:


A note about this...awk/gawk has lots of abilities that I don't know about may be possible to get awk to format the number the way you want with printf or whatever.