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:The option is also valid on
ext3
andext2
.Why it isn't done on-the-fly, I can't say. Maybe for performance issues?