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

5,685

Solution 1

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

Solution 2

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
Share:
5,685

Related videos on Youtube

Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on September 18, 2022

Comments

  • Tim
    Tim almost 2 years
    $ 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?

    • 炸鱼薯条德里克
      炸鱼薯条德里克 over 5 years
      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.
    • Ulrich Schwarz
      Ulrich Schwarz over 5 years
      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.
  • Ed Grimm
    Ed Grimm over 5 years
    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.
  • 炸鱼薯条德里克
    炸鱼薯条德里克 over 5 years
    I think most non-directory is also reported as size 0 on procfs or sysfs.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 5 years
    @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
    Sergiy Kolodyazhnyy over 5 years
    @炸鱼薯条德里克 Yes, that's correct. Because that data literally occupies 0 blocks of disk space.
  • Ed Grimm
    Ed Grimm over 5 years
    @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.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 5 years
    @EdGrimm I wasn't referring to whether or not he said it's a bug, but rather to the second part of what you said - lack of advanced features, which according to Theodore is not the reason and is doable. But if we're going to discuss bug vs non-bug, IMHO, just because no-one expected it doesn't mean it's proper behavior ( and improper behavior does qualify as a bug ). If you truncate a file, it shrinks in byte size. Same should be with a directory. Instead, it grows one way alright ( when data in the directory grows) but doesn't shrink when data decreases.