0

As long as I've been living in *nix world I've lived under the assumption that when a command accepted a -h flag the only thing I was changing was effectively formatting of a print statement. I don't usually re-examine this, but have definitely found this to be the case in general.

Today I had a very strange experience when measuring the size of a directory that I was actively writing to.

>>> du -s
15565160    .
>>> du -sh
7.4G    .

At first I thought that I was just catching the directory in a strange place, but I found that both values were quite persistent, returning only slightly increased/decreased values as I kept writing to the directory. My first assumption was that it was some strange intermediary state, but in that case I would have expected wild swings in the result of du -s that I did not observe.

What's going on here? What is the 155... number? It doesn't seem like it's bytes. There seems to be a pretty consistent ratio between the two of about 450. That is, if I multiple the result of du -s by 450 I get pretty close to the number of bytes implied by du -sh.

The bigger question is if I've been wrong all along. Why is adding the -h flag in this context doing anything other than changing the print format?

4

First off, you're correct. the -h flag (when not used to print help text) is used to tell various applications to convert the output to "human readable format." This is important... we'll come back to it. For du in particular the -s flag gives a summary of its normal "per-directory" output. Basically, it adds it all together.

du defaults to returning the number of logical blocks a directory and its contents occupy on the storage media. The count of blocks occupied is useful for many disk utilities and scripting... but it's not very "human readable." The -h flag converts the block count back into a number of bytes, then rounds to the nearest factor of 1024 (and a few other shenanigans... we end up with gigabytes and one digit past the decimal in your case).

Given what you've said, I'm willing to bet your system is configured for a 512-byte block size because:

15565160 * 512 / 1024^3

It's important to note that du assumes the block size is equal to (in order), the value set by the --block-size= flag, the value of the DU_BLOCK_SIZE, BLOCK_SIZE, or BLOCKSIZE environment variables, or 1024. 1024 bytes is the common default block size used by most modern file systems. But 512 byte blocks is not particularly rare. Check these variables, one of them is probably set to 512. Or you may have a particularly old version of du.

  • This explains it! Thanks so much. Had no idea that du defaulted to block size. Assumed that the bytes from ls were consistent, but clearly a bad assumption – Slater Victoroff Jun 26 '18 at 14:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.