Close vote notice: While the linked duplicate asks same question, the comments and the only answer do not provide an answer which draws from authoritative source. The answer on my question by Thomas Dickey, does in fact provide the answer I've been seeking. Thus the proposed duplicate isn't helpful, however Thomas's answer here is proper.

According to the answer by G-Man on Superuser question and personal account in this PerlDuck's comment, directories that have large amount of entries get to a size over 4096 bytes (which can be seen with ls -l output), but once the entries are removed - the number never goes down.

The question is "why" ? Is it due to how ext4 filesystem configured to retain directory metadata ? Obviously removing the directory and recreating it isn't a solution, since it deletes original inode and creates a new one. What can be done to decrease the number manually ?

  • not really - this question was specific enough to quote an authoritative source, unlike the suggested duplicate – Thomas Dickey Jan 17 at 22:42
  • 1
    @Kusalananda I've edited the answer to include a rationale for why the linked duplicate isn't appropriate. I would suggest linking the other way around, ans Thomas's answer here does in fact answer the why of it for both mine and the other question – Sergiy Kolodyazhnyy Jan 17 at 22:57
  • 1
    It is also worthwhile to note that a patch was recently submitted to allow ext4 itrctories to shrink as their leaf blocks become empty: patchwork.ozlabs.org/patch/1025534 – LustreOne Jan 19 at 17:12

Quoting a developer (in a linux kernel thread ext3/ext4 directories don't shrink after deleting lots of files):

On Thu, May 14, 2009 at 08:45:38PM -0400, Timo Sirainen wrote:
> I was rather thinking something that I could run while the system was  
> fully operational. Otherwise just moving the files to a temp directory +
> rmdir() + rename() would have been fine too.
> I just tested that xfs, jfs and reiserfs all shrink the directories  
> immediately. Is it more difficult to implement for ext* or has no one  
> else found this to be a problem?

It's probably fairest to say no one has thought it worth the effort.
It would require some fancy games to swap out block locations in the
extent trees (life would be easier with non-extent-using inodes), and
in the case of htree, we would have to keep track of the index block
so we could remove it from the htree index.  So it's all doable, if a
bit tricky in terms of the technical details; it's just that the
people who could do it have been busy enough with other things.

It's hasn't been considered high priority because most of the time
directories don't go from holding thousands of files down to a small

                                                - Ted

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.