Linux – du and ls report different directory size

dulinuxls

In trying to verify that a copy-operation completed successfully (or at least that it didn't skip any files), I ran du -b inside the source and destination directory and it showed a difference of about 100KB.

So, I went tracking down the difference and found one (of many) leaf-directory where du reports a difference in total size for the copy and the original.

The strange thing is that ls -l shows these directories as perfectly identical!

Here are the two outputs in the two different directories:

root@...:/local/.../DCIM/100___12# du -b
5286222389      .
root@...:/local/.../DCIM/100___12# ls -l --block-size=1
total 5292851200
-rwxr--r-- 1 markus markus   2167504 Aug  5  2013 IMG_0004.JPG
-rwxr--r-- 1 markus markus   2236594 Aug  5  2013 IMG_0005.JPG
...

vs.

root@...:/local_old/.../DCIM/100___12# du -b
5286226485      .
root@...:/local_old/.../DCIM/100___12# ls -l --block-size=1
total 5292851200
-rwxr--r-- 1 markus markus   2167504 Aug  5  2013 IMG_0004.JPG
-rwxr--r-- 1 markus markus   2236594 Aug  5  2013 IMG_0005.JPG
...

Note how the size reported by du -b is less than that reported by ls -l and that it differs for the two directories. The entire output of ls -l is identical for both directories.

There are no symlinks or system files in this directory. It's just a bunch of .jpg files (a direct copy off a camera's SD-card) and a Thumbs.db-file that Windows created for them (via samba)…

Am I missing something about how these commands should work?

(I'm running Debian Wheezy and uname -a outputs Linux ... 3.2.0-4-amd64 #1 SMP Debian 3.2.63-2+deb7u1 x86_64 GNU/Linux)

Best Answer

Both are right.

  • ls shows the exact bytes of the files
  • du is showing the disk usage, that is different because the system store the files using blocks.

You can find the block size your file system is using with this command:

blockdev --getbsz <partition>

For example:

blockdev --getbsz /dev/sda1