# Linux – Why is the size of a directory always 4096 bytes in unix

linuxshellunix

I am sure a directory file has much less information than 4096 bytes. I know the sector size is 4096 bytes. But normal files smaller than that do exist.

Why does Unix reserve 4096 bytes for each folder?

It's the initial size necessary to store the meta-data about files contained in that directory (including names). The initial allocation equals the size of one sector, but can grow above that if necessary. Once allocated, space is not freed if files are removed, to reduce fragmentation.

For example:

$mkdir testdir$ cd testdir
$ls -ld . drwxr-xr-x 2 matthew matthew 4096 2007-12-03 20:28 ./$ for ((i=0; i<1000; i++)); do touch some_longish_file_name_$i; done$ ls -ld .
drwxr-xr-x 2 matthew matthew 36864 2007-12-03 20:29 ./
$rm some_longish_file_name_*$ ls -ld .
drwxr-xr-x 2 matthew matthew 36864 2007-12-03 20:29 ./
$cd ..$ ls -ld testdir
drwxr-xr-x 2 matthew matthew 36864 2007-12-03 20:29 testdir/
$rmdir testdir ; mkdir testdir$ ls -ld testdir
drwxr-xr-x 2 matthew matthew 4096 2007-12-03 20:29 testdir/


source