Saturday, January 31, 2015

The meaning of the size of directories in long ls listing

One thing folks new to linux run into is what it means when they see a size associated with a directory in a long listing, and many assume that it represents the amount of space taken up by files in the directory - but that quickly becomes apparent that is not the case. So, to explain the directory size as it shows up in ls output, let's have an example:

24-rocket:~> mkdir derp
25-rocket:~> ls -l derp
total 0
26-rocket:~> ls -ld derp
drwxr-xr-x 2 cmh cmh 4096 Jan 30 17:44 derp

I've created a directory called "derp" and we can see that it's 4096 bytes - 4kb.

27-rocket:~> for x in {1..10000}; do touch derp/$x; done
28-rocket:~> ls derp | wc -l
10000

I now created 10,000 empty files in derp - none of them contain anything.

29-rocket:~> ls -ld derp
drwxr-xr-x 2 cmh cmh 155648 Jan 30 17:44 derp

Notice the size of derp is now larger - because derp contains the info for those files, such as filenames, inodes, etc. Interestingly enough, zero-byte files don't take up any disk space except their entry in the directory itself.

30-rocket:~> rm derp/????
31-rocket:~> ls derp | wc -l
1000

I just removed all of the four-digit files from derp, so that would be 1000-9999. We now have only 1000 files in derp.

32-rocket:~> ls -ld derp
drwxr-xr-x 2 cmh cmh 155648 Jan 30 17:45 derp

However, even though we've gotten rid of most of the files in the directory, it's the same size. Directories don't shrink. (At least not that I know of.)

One way to think of directories is that they're just files - special files that contain info about other files.That's why a zero byte file takes no space on the disk - it's just an entry in the directory file. Once you put even a single byte into a file, then there's disk space being used.

In order to get the amount of disk space used in a directory, you'll want to use the "du" command. To see the size of a single directory, run this command:

du -hs {directory}

In order to show the size of all subdirectories and files in the current directory, sorted by size, use:

du -ms * | sort -n

 Replacing the -h (human readable output) with -m (output in megabytes) makes all the numbers consistent so things sort properly. Otherwise, 900kb looks like more than 4tb - and that's not quite the case!


No comments: