Troubleshooting Disk Usage in Linux
At times, your server's disk(s) may begin to fill up with various files and folders. Rather than continue to scale the server up to make room, it may be best to take a look at what's using up so much space in your filesystem. This quick guide will help walk you through some of the steps and commands you can use to troubleshoot and reclaim some of your storage space. This article deals with Linux servers specifically, so if your server is a Windows server, you'll want to look at this guide: https://community.spinup.com/t/m2937l/troubleshooting-disk-usage-in-windows
• Server running a Linux based OS
• SSH access to the server
1) Log in to the device via SSH.
2) Determine the amount of disk space available.
To determine disk space, run the following commands:
# df -h
This command will output the different devices that make up your server's filesystem, and provide the percent utilized, as well as the total storage space, space used, and space free.
# du -h
This command is used to estimate file space usage. For example you could run du -h /etc to list the space used by the contents of the /etc directory. It's possible to string out more complex commands which can sort and make determining what's using your disk easier as well. Checking the 'man' pages for the du command will help you identify flags which can be added on to help. An example would be:
du -hs * | sort -rh | head -10
In this command we are asking du to do the following:
du = Estimate file space usage.
-h = Flag to make output in human readable format (ex, MB/GB/K).
-S = Do not include size of subdirectories.
-s = Display only a total for each argument.
sort = sort lines of text files.
-r = Reverse the result of comparisons.
-h = Compare the human readable numbers (ex., 1MB, 2K).
head -10 = Output the first 10 entries collected and sorted.
As you become more familiar with the flags, you can adjust and change the commands to your needs.
3) Clean up the drive.
Be careful when removing files and folders in Linux. Unlike Windows, they do not enter a Recycle bin where you can restore them, and using the wrong flag in some cases will result in all files and subfolders in a directory being removed.
To remove specific files, use the following command where $filename is the name of the file you want removed:
# rm $filename
You can delete multiple files at once by separating the filenames with a space. For example:
# rm $filename1 $filename2 $filename3
To forcibly remove files without any sort of prompt, add the '-f' flag to the rm command. For example:
# rm -f $filename
To remove an empty directory, use the rmdir command. Example:
# rmdir $directoryname
If the directory is not empty, but you want it removed along with all of it's subfolders and file, add the -r flag.
# rmdir -r $directoryname
Again, if you wish to avoid being prompted, you can add the -f flag. Example:
# rmdir -rf $directoryname
Once you've made the deletions of files/folders no longer needed, check your disk space utilization again with the 'df -h' command.