1

I ran this on my CentOS 7.3 instance.

[user01@ ~]$ rm -rf my-very-own-directory/
[user01@ ~]$ mkdir my-very-own-directory/ 
[user01@ ~]$ stat my-very-own-directory/ | grep "Size"
  Size: 6           Blocks: 0          IO Block: 4096   directory
[user01@ ~]$ mkdir my-very-own-directory/00
[user01@ ~]$ stat my-very-own-directory/ | grep "Size"
  Size: 16          Blocks: 0          IO Block: 4096   directory
[user01@ ~]$ date > my-very-own-directory/date.txt 
[user01@ ~]$ stat my-very-own-directory/ | grep "Size"
  Size: 32          Blocks: 0          IO Block: 4096   directory
[user01@ ~]$ echo "content" > my-very-own-directory/content
[user01@ ~]$ stat my-very-own-directory/ | grep "Size"
  Size: 47          Blocks: 0          IO Block: 4096   directory
  • Initial creation => size = 6
  • Add an entry with 2 bytes in name length => size is 16 (6 + 8 + 2)
  • Add another entry with 4 bytes in name length => size is 32 (16 + 8 + 8)
  • Add another entry with 7 bytes in name length => size is 47 (32 + 8 + 7)

2 Questions:

  1. A created directory has a size of 6. Why?
  2. Every new entry to the directory, be it file of a subdirectory will add to the size of 'my-very-own-directory'. The question is, the size increases by 8 + (length of the file name / directory name). Why 8?

1 Answers1

3

The directory format depends entirely on the filesystem.

In the least, a directory entry must contain the filename, and an inode number.

To use the Linux ext4 filesystem (and older versions) as an example, the original directory entry had the inode number (4 bytes), file name length (2 bytes), and total entry length (2 bytes), plus the file name itself. (IIRC the total length is specified explicitly to allow for padding and such.)

Current versions (from ext3) also support a tree-type storage, which allows faster lookups to directories with large numbers of files, but of course has a more complicated format.

Though on ext4, the size of a directory is shown only as a multiple of the block size, i.e. an empty directory has size 4096 and it doesn't increase until enough files are created to require allocating a new data block for it.

RHEL 7 uses XFS as the default file system, so that may be what you're running. XFS has a number of directory formats, and the short form used for small directories has essentially the same minimum set of inode number + file name + file name length + total length.

ilkkachu
  • 138,973
  • The thing is, inode number + file name + file name length + total lenth still does not explain why the initial size is 6 for "my-very-own-directory". The file name alone is more than 6 bytes. – Titi Wangsa bin Damhore Apr 08 '17 at 10:19
  • @Titi, the name of the directory itself is stored in the containing directory, not inside itself. There's a mention of a 6 byte header in that XFS document, which matches the size of your empty directory. – ilkkachu Apr 08 '17 at 10:28