Introducing dirsize

Sunday, 10 Apr 2005

For a long time, I’ve been using various doodads to achieve the following effect on my shell prompt:

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

I’m talking about the part of the prompt which shows the aggregate size of all files in the current directory, in human readable form. Unfortunately, none of the existing tools in the Unix set does exactly what I need to implement 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.1GB and 2.8GB.

The obvious solution is to 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 using a one-liner. 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: here’s dirsize, a small utility written in C that does exactly this (and a useful tiny bit more, since we’re at it).