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.

Mathieu Longtin
  • 321
  • 2
  • 6
  • 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
  • Nope, files were closed at deletion time, and whatever process that might have kept them open has been restarted. – Mathieu Longtin May 14 '12 at 20:19
  • So xfs does fix that problem. Nice. – Mathieu Longtin May 14 '12 at 20:20
  • 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 01:12
  • Also see http://stackoverflow.com/questions/4303008/how-to-defragment-a-directory-on-ext3 – oliver Jan 20 '17 at 13:51

2 Answers2

9

You run e2fsck -D on the unmounted filesystem.

psusi
  • 17,007
  • 3
  • 40
  • 51
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.

pva
  • 151
  • 2