It is a bit annoying when your disk space gets full, and you have no idea what is eating up the disk space, well, let me introduce you to du
command, with this command you can get the disk usage of the set of FILEs, recursively for directories.
Using du against directories and subdirectories will help you narrow down what and what is eating up disk space, please note that the more the files and sub-directories that are within your current working directory, the longer it would take to scan through, so, if you can guess the location of what is causing the issue, you might start from there.
To get started, you can run du -h
in your working directory, the problem with this is that, it would scan and calculate the disk usage of files recursively, and also show you the size of each and every file recursively. To consolidate that, you use, du -hsc *
user@server: du -hsc *
139M file1
25M movies
2.6G users
2.8G total
The -h option gives us a human-readable output (megabytes, gigabytes, and the likes), -s option gives us a summary instead of outputting all the files scanned, the -c shows the total amount of space usage within the current working directory, and lastly, the * prints out the name of the directory, if you are scanning directories and sub-directory, it only shows the size of the directory, and if you are only scanning through files, it would print out the sizes of each file.
Also, it gives you the total disk usage beneath the scanned directory or files.
To scan the home directory, you use the following command:
du -hsc * /home
Note: The du command is only able to scan directories that its calling user has permission to scan. So, you might want to scan using root to get the full sizes.
To find the 5 largest directories in your server, you use the following command:
Note: This would only scan the working directory, so, you might want to navigate into a directory that is worth scanning.
du -hsc * | sort -rh | head -5
Replace 5 with an higher number if you want to find more.
To find the 5 largest directories in a specific path or directory, use the following command:
du -hsc * /home | sort -n -r | head -n 5
I am using home as an example, so, you can change that to whatever directory.
To find the 3 largest files in your server, you use the following command:
find -type f -exec du -Sh {} + | sort -rh | head -n 3
To find the 3 largest files in a specific path, you use the following command (I am assuming /home):
find /home -type f -exec du -Sh {} + | sort -rh | head -n 3
Enjoy!