Finding Disk Space
recipe June 21st, 2008
Introduction
How many times have you had your web server stop serving web pages because it was out of disk space? Well, it seems to happen to me quite often. That is why I thought I would put this post together to help find the culprit that’s eating up my disk space.
Determining where you need space
The first thing you need to know is “where do I need space?” To find out where (which partition/disk) needs the space use the following command:
df -h
The “-h” gives you a human readable format. So you see results like 29M for 29 megabytes versus stuff like 30234524. It is much easier to read the results with the “-h”.
Now take a look at the given results from the ‘df’ command to see which partition needs space. Here is a sample output:
| Filesystem | Size | Used | Avail | Use% | Mounted on |
|---|---|---|---|---|---|
| /dev/sda3 | 31G | 23G | 5.6G | 81% | / |
| varrun | 697M | 256K | 696M | 1% | /var/run |
| varlock | 697M | 0 | 697M | 0% | /var/lock |
| udev | 697M | 88K | 696M | 1% | /dev |
| devshm | 697M | 12K | 697M | 1% | /dev/shm |
| /dev/sda6 | 24G | 16G | 7.4G | 68% | /home |
As you can see my root partition (”/”) is 81% used. If I was having disk space troubles, this figure could be as much as 100%.
Finding the Big Files
With the information about where we are running low on disk space from the section above, change directories to the start of that partition using the ‘cd’ command as follows:
cd /
If my root partition was low on disk space that is the partition I would go to.
Now, I am ready to determine which files are taking up the majority of my disk space. Use something like the following command to accomplish this:
find . -size +20M -exec du -h \{\} \;
This command will first find file of size 20MB or larger. Next it passes that file over to the ‘du’ (disk usage) command to show us in human readable format (-h) the size of the file.
That is quite a handy little command.
Removing Large Numbers of Files
Sometimes you may find that you have a directory with a large number of files that you wish to get rid of. However, the normal method of using the “rm” (remove) command comes back and complains that the list is too long. What do you do now? Well, don’t despair use the following command to solve this dilemma:
find /tmp -type f -exec rm -f \{\} \;
Be very careful using a command like this. This one if JUST an example. Read the man page on ‘find’ to see just how useful this command really is. This particular commandline will find all files (-type f) in the /tmp directory and pass the filenames to the “-exec” command. In this case the “”exec” command is the remove command (rm). The “-f” forces deletion quietly. The curly braces are replaced by the find command with the filename being passed in. The semicolon (with the backslash in front) is the required end of command marker.
Luckily the find command is pretty quick and VERY useful!
Tags: system admin

Leave a Comment