4

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).

1
1

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.

1

That's a feature of the ext2/ext3/ext4 filesystems, they report the size of directories in full blocks. If you try the same with XFS, you'll see a more fine-grained size.

It doesn't matter, really. For files, the "logical" size is relevant, since an application may depend on knowing the size of a file up to a byte: trailing garbage may be relevant, or some data structure may be placed at a position relative to the end of the file (e.g. .zip archives).

For directories, the "logical" size doesn't matter, since the internal structure is well-known, and on Linux, a user-space program can't even read the contents of a directory directly. Instead, a read has to go through system calls made for just that purpose. And those system calls will deal with the internal structure of the directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.