15

I'm using this command to determine which directory is eating my disk.

du -sk * | sort -n

How can I get human readable result form du for file sizes? I've checked man and all it have is -k flag which turns byte results to kilobyte results. I need results in gigabytes

user
  • 28,901
Mohsen
  • 2,585

2 Answers2

15

This may work:

du -hs * | sort -h

If your copy of du doesn't support the -h flag, then you can convert the numbers using awk.

du -ks * | awk '
function human(x) {
    s="kMGTEPYZ";
    while (x>=1000 && length(s)>1)
        {x/=1024; s=substr(s,2)}
    return int(x+0.5) substr(s,1,1)
}
{gsub(/^[0-9]+/, human($1)); print}'
  • 1
    my du supports -h. works good but sort doesn't work as you expect now. 90G is smaller than 100K now! – Mohsen Jul 01 '13 at 19:08
  • Are you sure sort -h does not work? I use it all the time in combination with du – Bernhard Jul 01 '13 at 19:31
  • 2
    my sort doesn't have -h. I'm on OSX – Mohsen Jul 01 '13 at 20:53
  • 1
    I should have made a note about that. The -h flag isn't very portable. You can use the awk option and sort before humanizing the numbers. Or, take a look at this: http://unix.stackexchange.com/a/4688/26112. –  Jul 01 '13 at 20:55
3

On a Linux machine [Debian based], I get this when opening the man page for du:

 -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

Thus: du -h should give you what you need. Else, also from man du:

   --si   like -h, but use powers of 1000 not 1024

   -k     like --block-size=1K

I really wonder where you found your information.

erch
  • 5,030