Linux – Fast way of knowing real directory size on linux/bsd

bsddirectorylinux

What is the fastest way of computing real directory size? I somehow catch myself needing that a lot.

Simply doing:

# du -hs /dir

Is too slow. Is there any service I could run which would periodically compute directory sizes and cache them for later reference? (something like a locate database)

Best Answer

Sadly I don't know of any, but it shouldn't be too hard to write one. Once a night, run

# du -a / > /var/lib/filesizes.txt. 

Then you just need a small script to sum those up. Something like:

# perl -ne 'BEGIN { $total = 0 } if ($_ =~ m/(\d+)\s+\/var\/www\//) { $total+=$1;} END {print "$total\n";}' /var/lib/filesizes.txt

If you want something a little more in sync, then you're gonna have to start writing something that uses inotify, to find out when the filesystem changes and updates a database, which would probably be something like bdb.