0

trying to understand the basics of du command is not that simple for me . For instance there is this -h option which is defined as printing in "human readable format" . The problem is that this option changes the displayed value :

~$ du --apparent-size -sh Documents/
376M    Documents/
~$ du --apparent-size -s Documents/
384767  Documents/

normally 384767 should be rounded to 385M. So which is the correct value 376M or 385M ?

Even worse :

~$ du -bsh Documents/
    376M    Documents/
~$ du -bs Documents/
    394001294   Documents/

1 Answer 1

1

normally 384767 should be rounded to 385M

If you divide by 1000, yes, but that's not what du is showing you. It's using the IEC (binary) units where 1 kB = 1024 bytes; 1 MB = 1024 kB; and so on. Some other programs would show these as KiB, MiB, etc.

(Most storage works with blocks that are some power-of-2 size, e.g. 512b or 4096b sectors, so IEC binary units are a somewhat better fit than SI decimal units.)

The default unit for 'du' without the '-h' option is 1024-byte blocks (which equals 1 kB), so it does 384767 / 1024 = 375.749… which is rounded to 376 MB (or MiB if you prefer). Similarly, when you use the '-b' option to start with bytes, it's 394001294 / 1024 / 1024.

Add the --si option if you want the decimal units (the "HDD manufacturer" units). This also works with df -h and ls -lh (i.e. tools from 'coreutils'), although not necessarily with other programs – some just always use IEC units without an option to switch.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .