Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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
show 2 more comments

1 Answer

You run e2fsck -D on the unmounted filesystem.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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