This is a completely useless, practically unnecessary, but amusing little post about directories in *nix systems. It's Friday after all.
During interviews, boring questions about inodes and everything-is-a-file often come up, which few can answer clearly. But if you dig a little deeper, you can find some interesting things.
To understand the post, here are a few points:
- everything is a file. a directory is also a file
- an inode contains metadata about the file, but the filename is not stored there
- the filename is stored in the directory data
- the size of the directory, which is displayed in ls and is by default 4KB, depends on the number of files in the directory and the length of their names
- obviously, the more files there are, the larger the directory size
Now, the interesting part: create a directory with a million files, check the directory size, then delete all files and observe the directory size.
$ mkdir niceDir && cd niceDir
# depending on the speed of the storage, the following command may take 2-10 minutes
$ for ((i=1;i<133700;i++)); do touch long_long_looong_man_sakeru_$i ; done
$ ls -lhd .
drwxr-xr-x 2 user user 8.1M Aug 2 13:37 .
$ find . -type f -delete
$ ls -l
total 0
$ ls -lhd .
drwxr-xr-x 2 user user 8.1M Aug 2 13:37 .As you can see, the directory size did not change, although it seemed like it should 🙂
To fix the directory size (without deleting) you can only use fsck (with the -D option) in an unmounted state.
When I went looking for why this happens, I found that 10 years ago this behavior was already discussed in lkml. And according to the developers, correcting it simply does not warrant the effort.
Source: habr.com
