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

giobbi

Registered
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..

:D

Thx.

p.
 
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:

"Library//Address","0"
"Library//Assistants","0"
"Library//Audio","0"
"Library//Autosave","0"
"Library//Classic","0"
"Library//ColorPickers","0"
"Library//Fonts","0"
"Library//Keyboard","0"
"Library//Phones","0"
"Library//Printers","0"
"Library//Sounds","0"
"Library//iMovie","0"
"Library//iTunes","0"
"Library//Documentation","8"
"Library//Internet","16"
"Library//Icons","24"
"Library//Workflows","48"
"Library//Recent","56"
"Library//Automator","64"
"Library//FontCollections","72"
"Library//Keychains","80"
"Library//Favorites","120"
"Library//Logs","136"
"Library//Mail","136"
"Library//Cookies","152"
"Library//Syndication","832"
"Library//MarbleBlast","864"
"Library//Safari","1504"
"Library//Preferences","3040"
"Library//InputManagers","3632"
"Library//Thunderbird","7488"
"Library//Widgets","14976"
"Library//Application","18568"
"Library//Mail","18728"
"Library//Caches","205776"
"Library/","276344"

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

Dale
 
Back
Top