Why are the sizes of 2 directories different if the data within the directories is identical? They are identical ext4 partitions and disks

root@phoenix:~# ls -ld /backups/external?/var/subsonic/thumbs/110
drwxr-xr-x 2 root root 73728 Mar  4 15:35 /backups/external3/var/subsonic/thumbs/110
drwxr-xr-x 2 root root 69632 Mar  4 15:35 /backups/external4/var/subsonic/thumbs/110

The contents of the above directories is identical. The disks and filesystem configurations are identical. Yet the sizes of the directories themselves are different.

Yes the contents of the directory in question are identical, i've checked with diff -r, rsync -avx as well as beyond compare. Here's diff -r output:

root@phoenix:~# diff -r /backups/external{3,4}/var/subsonic/thumbs/110
root@phoenix:~# 

du -bs output:

root@phoenix:~# du -bs /backups/external?/var/subsonic/thumbs/110
4116125 /backups/external3/var/subsonic/thumbs/110
4112029 /backups/external4/var/subsonic/thumbs/110

du -b --total output counting just the files within each directory:

root@phoenix:~# du -b --total /backups/external3/var/subsonic/thumbs/110/* | tail -1 
4042397 total
root@phoenix:~# du -b --total /backups/external4/var/subsonic/thumbs/110/* | tail -1 
4042397 total

So, for the external3:

4042397 + 73728 = 4116125

and for external4:

4042397 + 69632 = 4112029

The size of the files added to the size of the directory itself matches the output from du -bs.


Solution 1:

The size reported by ls -l for a directory has no relationship with the cumulated size of files stored in it.

It is just, for most common file systems, the size in bytes that has been required to store the directory entries. This size grows by block size (eg: 4096, 8192, ...). How many entries can be stored in a block is file system dependent and vary also with entries filenames length.

When directory entries are removed, the directory size is unaffected to reduce fragmentation.

In your example, the external3 directory has 18 * 4096 blocks allocated while the external4 has 17 * 4096 blocks.

That just means the former stored once more files (or files with longer names) even while the directory contents are currently identical. No big deal.