22

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.

8
  • 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, 2012 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. May 14, 2012 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, 2012 at 16:10
  • Have you by chance deleted open files?
    – Karlson
    May 14, 2012 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, 2012 at 1:12

2 Answers 2

9

You run e2fsck -D on the unmounted filesystem.

3
  • 1
    I was hoping for something less disruptive. May 15, 2012 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, 2012 at 15:07
  • I knew about that trick, but it can mess up processes using the directory as a current working directory. May 17, 2012 at 15:18
1

Not exactly an answer, but I think it's worth mentioning here, that there was a patch to fix this inability to shrink the directory:

https://www.spinics.net/lists/linux-ext4/msg71205.html

I hope it gets merged one day.

1

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.