filesystems – How to Update Directory Size After File Removal

directoryext4filesystems

This is mostly theorethical question without real practical usage.

As you may know, filenames are stored in directory inode. That means the more files we have and the longer filenames are the more space directory uses.

Unfortunately if files were deleted from the directory the space which is used by directory is not freed and is still used.

$ mkdir test ; cd test
# next command will take a while ; for me it was about 6 minutes
$ for ((i=1;i<103045;i++)); do touch very_long_name_to_use_more_space_$i ; done
$ ls -lhd .
drwxr-xr-x 2 user user 8.6M Nov  9 22:36 .
$ find . -type f -delete
$ ls -l
total 0
$ ls -lhd .
drwxr-xr-x 2 user user 8.6M Nov  9 22:39 .

Why the space used by directory isn't updated after the files removal?
Is there a way free the space without directory recreation?

Best Answer

You can optimize the directory using fsck.ext4 -D on an unmounted filesystem:

   -D     Optimize  directories  in filesystem.  This option causes e2fsck
          to try to optimize all directories, either by reindexing them if
          the  filesystem  supports directory indexing,  or by sorting and
          compressing directories for smaller directories, or for filesys‐
          tems using traditional linear directories.

The option is also valid on ext3 and ext2.

Why it isn't done on-the-fly, I can't say. Maybe for performance issues?

Related Question