Disk Usage – Why `du -b` Shows Different Size Than `ls`

disk-usagefilesystemsls

I have a directory with a few hundred files (real files, no symlinks, no subdirectories).

When I use ls -la and sum up the sizes in Excel I get 287190 bytes(?).

When I use du -b or du --apparent-size --block-size=1 I get 422358 bytes(?).

I thought those two things mean the same thing, why the difference?

Best Answer

du gives disk usage, which is not the same as the sum of all the file sizes.

For example, a du -b file will give a different output than making a directory "dir", placing the same file in "dir" and doing du -b dir. On my system that's 30 extra bytes for the "overhead" of a directory. Depending on the contents of the directory, I imagine the directory size would change (but I would be surprised if it was perfectly linear).

Also, the relative size of the difference implies that you might have missed a hidden directory with quite a few files in it, or that you might have missed a lot of hidden files (even though you did use the -a flag).

In addition, there may be symlinks which cause differences if one tool follows them while the other doesn't.

Finally, with some file systems, if the file's contents are small enough they might get inlined into the file system INode, and with many file systems, a single block is reserved to hold the contents of the file even if that block is not fully used. These variations add extra noise when attempting to compare the two.

Related Question