6

This question already has an answer here:

$ ls -l
total 28
drwxrwxrwx 1 t t 4096 May  8  2018  dir1
drwxrwxrwx 1 t t    0 Mar 12  2015  dir2
drwxrwxrwx 1 t t 4096 Jan 17  2014  dir3
drwxrwxrwx 1 t t 4096 Jun 12  2017  dir4
drwxrwxrwx 1 t t    0 Aug 24  2012  dir5

Why is the size of a directory either 0 or 4096?

Is the size of a directory 0 because the entries in it can fit into its inode?

Is the size of a directory not 0 because the entries in it can't fit into its inode and thus need some data block(s)?

Why is the nonzero size of a directory always 4096?

Thanks.

Note my observation is the same in ext4 filesystem and in NTFS filesystem. Does it mean both filesystem types implement directories in a similar way?

marked as duplicate by Olorin, Michael Homer, Jeff Schaller, Stephen Harris, Gilles filesystems Feb 26 at 12:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    They report similar size doesn't mean they are implemented similarly... How do you think like that... The reported size strongly depents on filesystem implementation, mostly meaningless, you can't conclude that the entries fit or not fit into inode data area just from the reported size. it's logic error. since at least you need to know the implementation of this filesystem. – 炸鱼薯条德里克 Feb 26 at 6:05
  • 3
    FWIW, it's not. Put enough files into the directory, and you'll see the size go up (probably in increments of of 4096 – a brief poke at my drive gave me 16384 (4×4096) and 69632 (17×4096) as other possible values. – Ulrich Schwarz Feb 26 at 6:18
7

Some filesystems have some space in the inode that can be used to hold very small directories. But once the directory is large enough to not fit in that area, the OS will allocate an entire file allocation unit, which in your case is apparently 4096 bytes.

On ext4, this is enabled with the inline_data ext4 option.

https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Inline_Directories

3

About 4096

To quote Terry Wang's answer:

A directory is just a special file which contains an array of filenames and inode numbers. When the directory was created, the file system allocated 1 inode to the directory with a "filename" (dir name in fact). The inode points to a single data block (minimum overhead), which is 4096 bytes.

Directories can grow larger than 4096, and one of the peculiar "bugs" of ext4 filesystem which did not receive much priority yet is that after directory information has outgrown 4096 bytes, that number is not decremented if files are removed. And probably the big news flash is that how information is allocated for a directory depends on the filesystem and even the options which are enabled/disabled( source ), so the special number 4096 is specific to ext4 in this case, but could be something else on other filesystems such as UFS for example.

Note, however, Terry's talking about inode which represents directory, directory itself being a dirent in parent directory. Initial directory contents are dirents or links . and ..


About 0

The ext4 filesystem defaults to creating directories ( even if empty ) with 4096 bytes allocation. That's the key. According to ServerFault and Arch Linux forum when directory shows as 0 in size it means the directory is located or hosts a filesystem other than ext4.

And this is true of virtual filesystems (which effectively aren't physical on-disk filesystems, but rather are an interface exposed by kernel and are contained in memory):

$ ls -ld /proc /sys 
dr-xr-xr-x 243 root root 0 Feb 24 13:57 /proc
dr-xr-xr-x  13 root root 0 Feb 26 14:42 /sys
  • 2
    That "bug" of ext4 was in ext, ext2, ext3, ufs, and a lot of other unix filesystem formats. It's not a bug, it's the lack of an advanced feature of a few filesystem formats. – Ed Grimm Feb 26 at 6:46
  • I think most non-directory is also reported as size 0 on procfs or sysfs. – 炸鱼薯条德里克 Feb 26 at 6:47
  • @EdGrimm Please look at the linked post, which features Theodore Tso's quote (who is one of the developers of ext4 ). It's not so much that there's lack of features - it can be implemented, but this has not received high priority from the developers. – Sergiy Kolodyazhnyy Feb 26 at 6:49
  • @炸鱼薯条德里克 Yes, that's correct. Because that data literally occupies 0 blocks of disk space. – Sergiy Kolodyazhnyy Feb 26 at 6:50
  • 1
    @SergiyKolodyazhnyy He didn't say whether it was a feature or a bug. He just said that ext4 doesn't do it. I think there's a few filesystems that supposedly do it that's not on his list, but there really aren't many that do, and they're all newer than ext2. Before them, it wasn't a feature that people particularly expected of filesystems because nobody did it. To get it added as a feature, it needs to be on the extn priority list, where n >=4. – Ed Grimm Feb 26 at 6:58

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