Managing Disk Space in a CentOS/cPanel Server


cPanel/Linux Disk Space Management

A common request that we see from our cPanel/WHM user is to remove files from the system to free up disk space. While we are certainly happy to oblige, there are some commands that can be run from within an SSH session to identify and remove those troublesome files.

Disk Space Usage Overview

First things first if you are encountering an issue with disk space usage, you will want to determine the total disk usage on the server. You can identify this value by running the following command:

df -h


Note: ‘df’ is the disk free command and with the addition of the ‘-h’ argument, we are identifying the usage and then making the output a bit easier to read.

These commands will give a very general overview but it will give you a place to start looking.

The next step will be to further dig into the file system to locate the directory that is using the space

Disk Space Management

Once you have determined a general idea of where a large portion of disk usage is occurring, you can proceed to further identify what directories or files are responsible.

Disk Usage Overview For Specific Directories

For disk space issues I recommend using the following command to identify disk usage from a higher level.

du -ah --max-depth=1 | sort -nr


Large File Location

Once you have narrowed down a few directories using large amounts of disk, I recommend running the following command to identify large files. Sometimes, however, the usage is due to volume, not size. Too many files can eat up disk space just like files that are too big.

find /path/to/directory -type f -size +25000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

This command will go through the defined path looking for 25MB+ files and output them for your review. File sizes this high are generally indicative of archives or bloated logs. You can also modify this command to run in other directories.

Note: If you do not find much in the /home directory, the /var, where a lot of system log files are located, is another common place that can eat up disk space due to bloated log and numerous audit files.

File Removal

Once you have located the source files you can then run the ‘rm’ command to remove the files in question.

 rm -f /path/to/file.extension

Note: the ‘-f’ argument is used to force the remove, otherwise you will be asked if you want to remove the file. While that is not a bad thing if you need to remove a number of files from different locations having an extra step can be a little tedious.

You can also clear all directory contents if there is a log or audit directory that is overwhelming the system.

Note: I strongly recommend caution with these commands as it will remove all content in the end directory of the path. Please ensure that your pathing is accurate before running this command. I also recommend using the -v argument which will display the files being removed.

rm -fv /path/to/directory/*.*
rm -rf /path/to/directory/

The ‘-r’ argument is used to invoke recursive removal meaning it will clear the contents of a directory without specifying an extension pattern.

Clearing Temp Files

Clearing the temp files for cPanel accounts is also another great way of freeing up disk space. Running the following command will allow for the system to go through the cPanel user accounts and remove content from the ‘/tmp’ directories. The content in this directory is not required for user function and is safe to remove.

rm -f /home/*/tmp/Cpanel_*