11

Today I found an "empty" directory with a size of 4MB.

It had no visible contents, so I tried ls -lah. This showed me some hidden files (not very large). Searching for the reason why the directory was so large I found that the dot file (.) had a size of 3.9MB.

What gets stored in that file? Isn't that just a kind of link to the same directory?

Here is the shell output (anonymized):

-bash# more /proc/version
Linux version 2.6.18-8.1.15.el5 (mockbuild@builder6.centos.org) (gcc version 4.1.1 20070105 (Red Hat 4.1.1-52)) #1 SMP Mon Oct 22 08:32:04 EDT 2007
-bash# pwd
/data/foo/bar/tmp
-bash# ls -lah
total 4.1M
drwxrwxrwx  3 nobody nobody 3.9M Nov 21 10:02 .
drwxrwxrwx 16 nobody nobody 4.0K Aug 27 17:26 ..
-rw-------  1 root   root    20K Oct 25 14:06 .bash_history
...
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

1 Answers1

15

The dot file, like every directory, contains a list of names for the files in this directory and their inode numbers. So if you once had lots of files in that directory (not unlikely for a "tmp" directory) that would have made the directory entry grow to this size.

After the files are gone, the file system doesn't automatically shrink the directory file again.

You can experiment with this yourself by making a new empty directory, do ls -la in it to see the initial size (4096 on my machine) then touching a lot of files, which will make the directory size grow.

(Yes I know that I'm glossing over/being inaccurate about a lot of details here. But the OP didn't ask for a full explanation of how EXT* file systems work.)

  • 2
    Thanks, this seems to explain the phenomenon. Two questions just out of curiosity: When would the directory file shrink again? And is there a way to show the contents of that file? – Martin Hennings Nov 21 '12 at 10:31
  • 3
    (1) See also http://unix.stackexchange.com/questions/38639/how-to-compact-a-directory - short answer, delete the directory and recreate it. –  Nov 21 '12 at 10:41
  • 2
    (2) Sadly, no longer. This used to be possible on old UNIX versions. –  Nov 21 '12 at 10:47
  • You can also run e2fsck -D on the filesystem, but that requires it be unmounted. – psusi Nov 21 '12 at 19:52
  • @Bristol debugfs may be insightful – Volker Siegel Jul 20 '14 at 05:56