Every so often, some application runs wild and fills a directory with a huge amount of files. Once we fix the bug and clean up the files, the directory stays big (>50MB) even though there's only 20-30 files in it.

Is there some command that compacts a directory without having to recreate it?

Bonus points: does a huge empty directory affect access performance of that directory? I'm assuming it does, but maybe it's not worth bothering. It seems slower to do ls on such a directory.

  • I don't think there is such a thing (but I could of course be completely wrong). Maybe some filesystem-specific tools though - what FS are you using? – Mat May 14 '12 at 14:26
  • ext3, but I've seen that problem on most filesystems I've dealt with in the unix world. I don't think there is such a tool either, but I'd like to be proven wrong. – Mathieu Longtin May 14 '12 at 16:03
  • 4
    I tagged your question "ext3" since this question is very filesystem-specific. I don't know the answer for ext3 (I suspect you need to recreate the directory to get it to shrink) but have you tried to force an fsck? ext3's fsck has an "Optimizing directory" pass. I don't know what that does, but maybe it shrinks directories that are too big? Anyway, as a data point FWIW, xfs autoshrinks directories that have had files removed from them. – Celada May 14 '12 at 16:10
  • Have you by chance deleted open files? – Karlson May 14 '12 at 18:47
  • 1
    ufs on the BSDs and Mac HFS both shrink directories on the fly also. I'm surprised to learn that ext3 doesn't. ext4 doesn't seem to do it, either. – Kyle Jones May 15 '12 at 1:12

You run e2fsck -D on the unmounted filesystem.

  • 1
    I was hoping for something less disruptive. – Mathieu Longtin May 15 '12 at 14:33
  • 1
    @MathieuLongtin, You also could move everything to a new directory, delete the old one, and rename the new one back. – psusi May 15 '12 at 15:07
  • I knew about that trick, but it can mess up processes using the directory as a current working directory. – Mathieu Longtin May 17 '12 at 15:18

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.