4

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?

rush
  • 27,403
  • How many directories contain 100K+ files? How often do you delete all of them? – Barmar Nov 09 '14 at 22:17
  • 1
    @rush - read Ted Tso's posts here – don_crissti Nov 09 '14 at 22:29
  • 1
    I'm not sure if you are correct in this statement "As you may know, filenames are stored in directory inode." The filenames are stored in as data in a directory file but not in the directory inodes itself. – mdpc Nov 10 '14 at 05:57

1 Answers1

6

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?

garethTheRed
  • 33,957