Linux – Why does “ls -l” report the physical size for a directory while it reports the logical size for a file

directoryfilesfilesystemslinuxls

I have notice that ls -l reports the physical size for a directory while it reports the logical size for a file. For example this is a sample output from ls -l:

drwxr-xr-x 2 chris chris 36864 2017-04-23 18:14 dir1 
-rw-r--r-- 1 chris chris 6     2017-04-23 18:10 file1.txt

dir1 size is 36864 bytes, which is a multiple of 4096 bytes (so it is probably the physical size).

While file1.txt size is 6 bytes (which is the logical size).

Best Answer

Actually, ls reports whatever the kernel tells it: it doesn't do anything different regarding the size depending on whether the file is a directory). And Linux reports the logical size. But the logical size of a directory is not a very interesting property: it depends on the filesystem format.

The default filesystem type on most Linux distributions is ext4, and that's probably what you're using. Ext4 allocates whole blocks for directories and manages space inside those blocks as it sees fit. It doesn't even release blocks if a directory shrinks (dir1 would keep having 9 blocks even if you removed all the files in it). When asked the size of a file that happens to be a directory, ext4 returns the size that's allocated for the directory, and this is always a whole number of blocks.

Different filesystem types behave differently. For example, with Btrfs, experimentally, the size of a directory can be any multiple of 2.