In Linux or more particular EXT4 the initial size of directory file is 4kB. But if a large enough number of files are stored within the directory the size of the directory file will increase due to the increase of the internal "file list". However, how many files are needed for this to happen? I have been unable to find a resource that can answer this question.
2 Answers
The format of ext4 directory entries is documented in the kernel. There are two possibilities.
- For linear directories, each entry occupies eight bytes, plus the file name (zero-terminated), rounded up to four bytes. So n file entries occupy 8 × n bytes plus the lengths of all the file names individually rounded up to four (including the terminating zero). Directories always include
.
and..
which occupy twelve bytes each. Each linear directory can also have a twelve-byte checksum. The last entry in a block has its record length extended to cover the remaining room in the current block, so that directory entries never straddle two file system blocks. - For hash tree directories, the first data block in each directory has a 40-byte root entry (which includes file entries for
.
and..
), and each subsequent data block has an 18-byte node. Nodes occupy eight bytes each, and file entries use the same data structure as in a linear directory, ultimately as a linear array. So the amount of space consumed by a directory is harder to compute: each file occupies eight bytes plus the length of its name, rounded up to four bytes, and the tree structure consumes 40 bytes for the first block plus 18 bytes per extra block, and eight bytes per node.
If you want to quickly see a directory increase in size, fill it with files with lengthy file names — file names can be up to 254 bytes in length, plus the terminating zero byte, occupying 264 bytes in total, so 16 such entries in either type of directory will require more than 4096 bytes.
To determine whether a directory is linear or hashed, examine its inode, e.g. using debugfs
:
debugfs: show_inode_info /path/to/directory
Inode: 7329 Type: directory Mode: 0755 Flags: 0x1000
Generation: 2283115506 Version: 0x00000001
...
The flags will show 0x1000 set if the directory is hashed, unset otherwise.

- 434,908
New directory:
mkdir newtemp
Size of new directory:
ls -l | grep newtemp
drwxr-xr-x 2 jsevans users 6 Jan 11 17:42 newtemp
Create a new empty file with touch
touch 1
Recheck size:
ls -l | grep newtemp
drwxr-xr-x 2 jsevans users 15 Jan 11 17:42 newtemp
Therefore 1 file is needed.
If this doesn't answer your question, then please review the full documentation of the ls command as per the man page.

- 201
-
OpenSUSE Tumbleweed – elmerjfudd Jan 11 '20 at 17:04
-
Sorry about that. Btrfs – elmerjfudd Jan 11 '20 at 17:05
-
1Thanks. The question appears to be about Ext4, not Btrfs... – Stephen Kitt Jan 11 '20 at 17:14