We all run low or run out of disk space every now and then. In fact, I’ve found as a sysadmin that this is one of the more frequent and annoying problems that pop up. Especially if you don’t have an easy way to figure out what is using all of that disk space.
I use HDGraph on Windows systems to do this and find it really helpful. I couldn’t live without it. Well I could, but I would have no storage left. There are some similar tools available for Linux desktops and Mac OS X also but on a headless Linux server all you have is the command line and your wits.
After a lot of googling, I found this great little command will help you find large files/folders on a Linux system. Well perhaps little is not the best choice of words, and it’s not super user friendly, but it does a great job.
The path in the command can be changed to focus on a specific folder. So to be very general you could start at / and then work your way into the biggest folder until you figure out where all that disk space has gone.
Show top 10 largest files or sub-folders in /var:
sudo du -a /var | sort -n -r | head -n 10
Show top 20 largest files or subfolders in the root folder /:
sudo du -a / | sort -n -r | head -n 20
Show the 11th-20th largest files in /var/log/:
sudo du -a /var/log/ | sort -n -r | head -n 20 | tail -n 10
The beauty of this command is that it uses standard utilities that are included with almost all *nix based operating systems including Linux and Mac OS X.
If you get this error then your hard drive is most likely 100% full and you may need to do some manual cleaning up first before you can start using the above command.
sort: write failed: /tmp/sort0uH5NO: No space left on device
Take a quick look in your home folder and in the log folders to see if there are a couple of files that you could do without. You should only need to clear a few KB of space before our handy-dandy command will have enough room to work with.
Home Directory:
cd /home/[username]
or simply cd ~
rm someoldfile.1
Log Folders:
cd /var/log/
rm someoldlog.5.gz