Checking Disk Space via Shell

// By Dillon Kavanagh

Prerequisite: This article is for server administrators/web developers that have a solid base knowledge surrounding their server environment.

Commonly your server will run out of space, easily you’ll just add on more dynamic SSD but not this time! It’s time to do some spring cleaning. This article will help you search through your server when you’re unable to connect to cPanel, WebMail or WHM. Hopefully you’re tackling the issue prior to the above issues and the symptoms of the issues are slow site speed.

When disk space is an issue there are a number of methods you can use to review remaining space;

  • WHM; Web Host Manager is a dynamic and powerful program that allows administrators access the back-end behind cPanel.
  • Shell Command Interface; Shell is the user interface for access to an operating system’s services. We are focusing our review today entirely through shell.

Viewing remainder disk space & current usage;

Running df – dh will output the disk usage via Shell Command you can also view disk usage via WHM. These controls allow you to keep up to date on disk usage appropriately.

df -h

The expected output should look like the following;

Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            196G  156G   31G  84% /
tmpfs                 3.9G     0  3.9G   0% /dev/shm
/usr/tmpDSK           3.9G   74M  3.6G   2% /tmp

The above showcases that 31 Gbs of disk space remains and that we have used 156GB of space. This leaves the server with 16% approximately free. If the server reaches 98-99% capacity there is a high risk of cPanel login failure along with FTP upload issues, slow lag pertaining to existing websites and more. This is a definitive sign you need to remove some of these files from the server to bring the server back to normal operation.

Removal of files with isolation to file size range

There are two commonly used search commands, my personal preference is “find” over “du”. Find is fit for purpose, however, I’ll give example uses for both.

Finding specific files via the “find” method

Below is the “find” command that looks for specific files in the “home” directory. The following script is finding files that are 500000k or larger;

Tip: 100000 is the equivalent to 100MB, it’s quite common in general coding practices that smaller values are inherently used to compare, search or denote value.
find /home -type f -size +500000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

The above command will output a result similar to below;

/home/user/public_html/wp-admin/error_log: 231M
/home/user/public_html/wp-content/uploads/2015/11/Module: 31M

The above shows there is an error log file taking up an excess of 200 Mbs of storage on the server. You should backup any files before removal and then delete them from the server. If you are having issues removing the files from your server you should contact your IT administrator or directly your hosting company.

Finding files with the “du” command

The following command will display all files and folders sorted by MegaBytes.

du --max-depth=1 | sort -n | awk 'BEGIN {OFMT = "%.0f"} {print $1/1024,"MB", $2}'

This output shows that all files over 1 MB are in the /public_html. You can run this command on a directory basis to find folders that are large. Also, this command can be customized to refine your search.

Tip: You can simply run “rm -rf path/to/file” from the terminal to delete the file and therefore removing any additional search processes.

Keep Reading

All Projects