Wednesday, 4 Nov 2009


This tool outputs the aggregate size of all files in one or more directories, defaulting to the current directory. It does not recurse.

usage: dirsize [-b] [-H] [-h] [-p precision] [dirname ...]
         -b  brief output, excludes dirnames
         -H  human readable, prettier size display
         -h  this help
         -p  (implies -H) number of fractional digits displayed [default: 1]

Its primary purpose is for use with the shell prompt, in which case I suggest adding something like $( dirsize -Hb ) to your PS1 environment variable. F.ex, mine ends with the following string:

\[\e[1;33m\]\w\[\e[;0;1m\] ($( dirsize -Hb )) \$\[\e[;0m\]

And the result looks like so:

~ (29.7M) $ cd /tmp
/tmp (141.9K) $ _


None of the existing tools in the Unix set does exactly this. du(1) cannot be prevented from recursing, which makes for a useless figure that will often take way too long to generate. ls(1) produces a suitable “total” line with human readable units, but unfortunately no fractional digits – and there’s a bit of a difference between 2.1G and 2.8G.

You can certainly attack this with a bit of scripting, and in fact, this is what I did for the longest time. But nicely human readable output is a minor pain to produce. Worse, when the script is called on for the display of every single prompt, the overhead introduced by any of the interpreters will be annoying on occasion.

That was a good enough excuse to brush up my C skills.


Version 0.6