How to: Manage your hard drive space

For WHM version 11.34

PICK Important: We strongly recommend keeping at least 10% of your disk space free.

Examining disk space usage by file system using the df command

If you suspect you're running out of hard drive space, you can easily confirm your suspicion using the df command.

df -h

In this example, the df command will return all of the file systems mounted on your server and basic information about each mounted file system. The -h argument causes the df command to print the results in a human-readable format. The results should resemble the following:

root@host [/var/log]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/LogVol00  288G  189G   85G  70% /
/dev/sda1              99M   47M   48M  50% /boot
tmpfs                 252M     0  252M   0% /dev/shm
/usr/tmpDSK           485M   11M  449M   3% /tmp

From this output, we can determine the system's main storage device (/dev/mapper/LogVol00) mounted at root (/) is at 70% capacity. In this case, there are only 85 gigabytes of free space on the system's main storage device.

Examining disk space usage by file and directory using the du command

To find out which files and directories are consuming most of your hard drive space, you can use the du command. This command will print the estimated disk space usage of each file and directory specified.

du -sh /

In the example above, the -s argument will print a summary of all contents of your present working directory, versus printing each file and directory's disk usage information individually. The h argument (as when using df) causes the command to print the information to the CLI in human-readable format. The slash (/) in this command specifies that you want to examine the contents of the root directory.

When passed in this way, the du utility will print the estimated disk space of each file and directory contained within your root directory (/).

The output of this command should resemble the following:

root@host [~]# du -sh /
372K   ~
107M   etc
113 G home
253M   lib
20K   LICENSE
2.6M   locale
16K   lost+found
64K   mbox_backup
8.0K   media
8.0K   mnt
418M   opt
0   proc
12M   pub
4.0K   pub-htaccess.txt
648K   templates
14K   tmp
64K   tools
5.4G   /var

The du command will produce more output than the example above. Ideally, you should only run this command during off-peak hours to prevent putting additional load on your hard drives. You should also keep in mind that the du command is slow by its very nature. This is because it calculates the amount of disk space each file and directory uses on the fly.

Once we have the output information, we can examine the numbers in the far left column. These numbers represent the sizes of the files and directories contained within the working directory (in this case, /), in human-readable format.

After examining the output above, we can determine that the /home directory is using the most disk space: 113 gigabytes. However, it is very likely that your cPanel accounts are contained in that directory. As it is unlikely you will be able to remove your users' data, let's examine the /var directory instead, by running another du command.

root@publicdocs [~]# du -sh /var/*
12K	/var/account
4.0K	/var/aptitude
28K	/var/aquota.user
938M	/var/cache
559M	/var/cpanel
4.0K	/var/cvs
24K	/var/db
32K	/var/empty
8.0K	/var/games
173M	/var/lib
8.0K	/var/local
36K	/var/lock
3.7G	/var/log
0	/var/mail
10M	/var/named
8.0K	/var/nis
8.0K	/var/opt
44K	/var/portsentry
8.0K	/var/preserve
144K	/var/profiles
16K	/var/proftpd
16K	/var/proftpd.delay
4.0K	/var/proftpd.pid
4.0K	/var/proftpd.scoreboard
8.0K	/var/quota.user
8.0K	/var/racoon
1.2M	/var/run
16M	/var/spool
4.0K	/var/state
14K	/var/tmp
4.0K	/var/vzquota
44K	/var/www
20K	/var/yp

In the example above, we've specified the /var directory to the du command and added an asterisk (*) to the end of the command. The asterisk is a wildcard that tells the du command to list the summarized, human-readable disk usage information for every file and directory in /var. After examining the output, we can determine that /var/logs/ is using the most disk space.

You can continue running du commands until you locate the larger files you wish to remove. For the purposes of this tutorial, we can skip ahead a few steps.

Removing unnecessary files using the rm command

To clear some disk space, we will need to remove some files. We can use the rm utility to do so. This utility removes files from the file system.

rm -f $file

In the example above, the -f argument forces the removal of the file. This means that you will not be prompted to confirm that you wish to remove the file. Passing rm using the -f argument will save time. However, you will need to be absolutely certain you are ready to delete the file . The $file parameter is the path to the file or directory you wish to remove. For our purposes, we will only remove single files, rather than entire directories.

Let's continue by removing an old audit log file. To do so, we could use the following chain of commands:

root@host [/]# cd /var/log/audit
root@host [/var/log/audit]# du -sh *
1.8M   audit.log
5.1M   audit.log.1
5.1M   audit.log.2
5.1M   audit.log.3
root@host [/var/log/audit]# rm -f audit.log.3 
root@host [/var/log/audit]# du -h *
1.8M   audit.log
5.1M   audit.log.1
5.1M   audit.log.2

As you can see in the example above, we have successfully located and removed a file that was consuming resources on the system. You can repeat this process until you clear enough free space.

Additional Resources

Topic revision: r13 - 25 Jan 2013 - 17:00:42 - Main.LaurenceSimon
AllDocumentation/WHMDocs.ManageHDSpace moved from Sandbox.ManageHDSpace on 30 Jun 2011 - 15:27 by Main.JustinSchaefer - put it back