The du command is a nice tool for telling you the disk usage of your files:

$ du *
1928	system.log
136	system.log.0.bz2
112	system.log.1.bz2
0	uucp
552	vnetlib

I find that adding the -h flag (which produces more human-readable output) is often very helpful for me:

$ du -h *
964K	system.log
 68K	system.log.0.bz2
 56K	system.log.1.bz2
  0B	uucp
276K	vnetlib

Now if I could just sort that…

$ du -h * | sort -n
  0B	./uucp
3.0M	./
4.0K	./eventmonitor
4.0K	./performance
5.9M	./asl

As you can see, this doesn’t work so well on OS X (as of 10.8.5). What can be done?

  • If you’re on Linux you probably have a recent version of the sort program, specifically v7.5 or later, which has its own -h flag:

    $ du -h * | sort -h
    76K     lightdm
    380K    apache2
    1.1M    installer
    1.4M    auth.log
    1.5M    cricket
  • If you’re stuck with something older, you can use a little utility like duhsort:

    $ du -hs * | duhsort
    704K    tests/pr
    928K    ChangeLog
    1012K   tests/misc
    1.1M    old
    1.5M    doc

    duhsort was really created to be used in shell functions and aliases. For example:

    $ duh() { du -h "$@" | duhsort; }
    $ duh | tail -5
    3.1M    ./gnulib-tests
    3.2M    ./tests
    5.9M    ./lib
     25M    ./po
     50M    .
    $ duh old doc
    244K    old/sh-utils
    328K    old/textutils
    548K    old/fileutils
    1.1M    old
    1.5M    doc
    $ duh -s old doc
    1.1M    old
    1.5M    doc