A directory file has typically 4096 byte. However, I have a couple of directory files which have a size of more than 4096 bytes, e.g. 28672, 12288, 20480, 16384 (for ext4). I read this article and it says: “The initial allocation equals the size of one sector, but can grow above that if necessary. Once allocated, space is not freed if files are removed, to reduce fragmentation.” This article doesn't quote how to solve the issue. Copying to a second directory and deleting the old directory could help. But I am looking for a simple command, which clean up the directory files.

I found the answer here:

e2fsck -f

The option -D can be omitted.

  • My first thought is that you don't actually want to do that, for exactly the reason your article indicated. non-contigiousness will likely cause more slowness than having your directory file occupy several blocks. – Frank Thomas Jan 5 '18 at 14:07
  • Possible duplicate of Why is the size of a directory always 4096 bytes in unix? – harrymc Jan 5 '18 at 14:13
  • Please read the man page of e2fsck for option -D. – musbach Jan 5 '18 at 15:19

Depending on the filesystem used you can move all the files to a different directory, remove the original directory, recreate it and move the files back.

In practice; are you really going to bother with all that for 20KB disk space?

  • I am not worried about the disk space, but I am worried about possible damage or slowness. – musbach Jan 5 '18 at 13:53

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.