At times, your server may fill up with logs, user-uploaded images, or other data. This can fill up your disk fast, and as a result that makes your server act up really weird. Services are acting out, unable to write data because of a full disk.
In this article, we'll write a few tips on how to manage your disk and find big files.
Use "ncdu"
This package makes it easy to navigate around directories on your server. It might be that ncdu isn't installed yet on your server. You can install it by running this command as root user:
apt install -y ncdu
Next, navigate to the "/" path and run ncdu:
cd / && ncdu
This will now scan your server, this can take a while. Just let it process and finish, once it is done you'll be presented with a list of directories that are sorted from biggest to smallest:
Navigating is rather easy, use the arrow keys, and enter key to go into directories and go back, to go back, use the arrow keys to go to the top where you see "/..", press enter on that line and you'll go back one directory.
You can now search for big files and delete them. To delete them, navigate with your arrow keys and press "d", this will give you a prompt if you want to confirm the deletion. To go out of ncdu, press the "q" key.
Find files bigger than {x}MB
It is also possible to run a command that will find files on your server that are bigger than the amount you put in.
For example, to find files that are bigger than 20MB you run the following command:
find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
This will scan in the directory you're in, so if you need to scan the whole server, navigate to the root first:
cd / && find . -type f -size +200000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
Good ol' du
This is also available by default, good old "du" which works for simple cases. Run the following command to get a list sorted by size of big directories:
du -hsx * | sort -rh | head -10
Which produces:
root@royal-reef:/# du -hsx * | sort -rh | head -10 2.2G usr 1.6G var 1.2G lib 1.1G swapfile 990M home 218M boot 45M root 16M bin 15M sbin 9.6M etc
To get a list of big files in side the default /home/ploi folder for example:
du -hsx /home/ploi/* | sort -rh | head -10
This will produce:
root@royal-reef:/# du -hsx /home/ploi/* | sort -rh | head -10 474M /home/ploi/ploi.shop 270M /home/ploi/ploi.io
Using df to list all disk sizes and usage
For most people, a very common command which is "df", allows you to list the disks and their usage. It is as easy as running:
df -h
Which will produce something in the lines of:
root@royal-reef:/# df -h Filesystem Size Used Avail Use% Mounted on udev 461M 0 461M 0% /dev tmpfs 99M 1.1M 98M 2% /run /dev/vda1 24G 7.1G 16G 32% / tmpfs 493M 0 493M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 493M 0 493M 0% /sys/fs/cgroup /dev/loop0 100M 100M 0 100% /snap/core/11993 /dev/loop1 62M 62M 0 100% /snap/core20/1270 /dev/loop2 44M 44M 0 100% /snap/certbot/1670 /dev/loop3 111M 111M 0 100% /snap/core/12603 /dev/loop4 62M 62M 0 100% /snap/core20/1328 tmpfs 99M 0 99M 0% /run/user/1000