Table of Contents
Find areas of disk usage with SSH
Updated Jun 8th, 2021 at 15:01 BST
The first step in investigating a disk usage issue is to find where the content is being consumed. This will allow you to remove unnecessary content (such as old backups) and make changes to prevent the issue from recurring. If you're comfortable using SSH on your server, you can find large files/directories and remove them over the command line.
Warning: If you don't know the purpose of a file or folder, don't remove it. Removing system files or directories is irreversible and may break the server (taking all of the sites down).
Switch to the root user.
For an overview of disk usage on the server, use the command "df -h". In the example, you can see that 35G of our 40G server is in use.
[root@server ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 909M 0 909M 0% /dev
tmpfs 919M 0 919M 0% /dev/shm
tmpfs 919M 17M 903M 2% /run
tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/sda1 40G 35G 5.7G 86% /
/dev/loop0 1.8G 2.9M 1.7G 1% /tmp
tmpfs 184M 0 184M 0% /run/user/1000
Find large files:
Oftentimes, there is a single large file or a few large files that are causing issues. You can find them by searching for files on the server that are over 500MB in size and then sorting the list with the largest files listed at the end.
find / -type f -size +500M -exec du -h {} + 2>/dev/null | sort -h
In our case, a large error_log file and some backup files were identified:
root@server ~]# find / -type f -size +500M -exec du -h {} + 2>/dev/null | sort -h
5.1G /home/onecool/public_html/wp-content/backups/coolexample_backup_1.tar.gz
5.1G /home/onecool/public_html/wp-content/backups/coolexample_backup_2.tar.gz
11G /home/onecool/public_html/error_log
Any large files can then be removed using the rm command, and confirmed by typing y (yes).
root@server ~]# rm /home/onecool/public_html/error_log
rm: remove regular file ‘/home/onecool/public_html/error_log’? y
[root@server ~]#
Find large directories:
You can locate large directories (not just single files) by using variants of the du command. To list the sizes of the directories from the server root (sorted by size), you can use these commands:
Change to the / directory
root@server ~]# cd /
Check the disk usage
[root@server /]# du -sh *
To list the 10 largest directories and sizes (including hidden directories) in the current directory, use this command:
[root@server /]# du -sh .[!.]* * | sort -h | tail -10
To find the 10 largest directories on the entire server (not including subdirectories), you can use this command:
[root@server /]# du -Sh / | sort -h | tail -10
Once you've identified a large directory, you can navigate to it, view it's contents, and remove any unneeded files.
Note: After clearing disk space, it's a good idea to reboot the server to make sure all needed services are properly restarted.