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?

  • How many directories contain 100K+ files? How often do you delete all of them? – Barmar Nov 9 '14 at 22:17
  • 1
    @rush - read Ted Tso's posts here – don_crissti Nov 9 '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 5:57

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?

  • 1
    Because most of the time it's not necessary. Unless you're deleting thousands of files, it's not going to reclaim a significant amount of space. – Barmar Nov 9 '14 at 22:15
  • 3
    Or if you don't want to unmount the filesystem to fsck it, you can just create a new directory, move the remaining files there, remove the original directory and rename the new one in its place. – psusi Nov 10 '14 at 1:49
  • 3
  • @Gilles - some seriously good Google skills or a brilliant memory there :-) – garethTheRed Nov 11 '14 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.