Watch out with du(1) and the block size it assumes
Tagged:  •  

Today I encountered this strange effect on a FreeBSD machine:

# du -s /some/dir
53982 /some/dir
# sudo du -s /some/dir
107964 /some/dir

When invoking du with sudo, it returns double the size of the actual size. It took me an hour or two to find out what was going on.

As always, I should read manpages more carefully:

BLOCKSIZE If the environment variable BLOCKSIZE is set, and the -k
option is not specified, the block counts will be displayed in
units of that size block. If BLOCKSIZE is not set, and the -k
option is not specified, the block counts will be displayed in
512-byte blocks.

So, that's why I got double sizes, it assumed 512 block size. It seems that BLOCKSIZE is not set in some environments, like when invoking sudo or from within a script invoked by a cron job.

Another thing learned, use -k explicitly when calling du(1) and everything will be fine.