Sort and Du -h command


Recommended Posts

hello guys

I'm using Ubuntu Linux and having some problem in the Terminal (first day using linux :shifty:)

when I use this command (executing the command in root)

sudo du -cs * | sort -rn

output

31444026		total
28595542		media
2178368 usr
245024  home
218436  var
167420  lib
17768   boot
9840	etc
6432	sbin
4836	bin

everything good so far but when i add -h for du command sort isnt doing good job

 sudo du -hcs * | sort -rn

output

240M	home
214M	var
164M	lib
136K	root
96K	 tmp
80K	 dev
30G	 total
28G	 media
20K	 opt
18M	 boot

is there a way to fix it like defining custom sort (G > M > K)? or any possible fix?

i really like the linux terminal :laugh:

Thanks in advance

Link to comment
https://www.neowin.net/forum/topic/610786-sort-and-du-h-command/
Share on other sites

Google says this:

  Quote
du -k | sort -nr | awk '

BEGIN {

split("KB,MB,GB,TB", Units, ",");

}

{

u = 1;

while ($1 >= 1024) {

$1 = $1 / 1024;

u += 1

}

$1 = sprintf("%.1f %s", $1, Units);

print $0;

}

'

www.unix.com

Maybe there's a more simple way to do that, but the script seems to work.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.