24-rocket:~> mkdir derp25-rocket:~> ls -l derptotal 026-rocket:~> ls -ld derpdrwxr-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; done28-rocket:~> ls derp | wc -l10000
I now created 10,000 empty files in derp - none of them contain anything.
29-rocket:~> ls -ld derpdrwxr-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 -l1000
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 derpdrwxr-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:
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!
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:
Post a Comment