There are many reasons you might want to do this, such as coming near capacity and not wanting to go through the pain of a hardware upgrade or simply that a partition was undersized when doing the initial install and want to try and prevent a complete reinstall.
Whatever the reason here is a few tips to free up some disk space in a Linux-based operating system.
EXT4 Stole 5% of my disk! #
First lets take a look at your disk and mount point layout with ‘df -h’
root@deploy:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 798M 81M 718M 11% /run
/dev/sda2 30G 17G 13G 57% /
tmpfs 3.9G 4.0K 3.9G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 511M 5.2M 506M 2% /boot/efi
/dev/sdb1 30G 24G 4.3G 85% /tempsdb
/dev/loop0 708M 708M 0 100% /tempsdb/iso
tmpfs 798M 0 798M 0% /run/user/0
As we can see in the example above the root ‘/’ partition has 30G of space, 17G used and,13G available which is 57% used.
If the filesystem is EXT4, as standard it will reserve 5% of the total disk space for that partition to protect the end user from truly running out of space which would be catastrophic for the operating system, this is an old standard inherited from past EXT filesystems when hard disks were much smaller generally so 5% can at times be overkill.
To reduce the amount of space reserved and make it available again you can run the following:
tune2fs -m
Example:
root@deploy:~# tune2fs -m 0.5 /dev/sda2
tune2fs 1.44.5 (15-Dec-2018)
Setting reserved blocks percentage to 0.5% (40035 blocks)
As we can now see there is less space used on the ‘/’ partition, we now have only 55% used disk space and 14G free space:
root@deploy:~# df -h Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 798M 81M 718M 11% /run /dev/sda2 30G 17G 14G 55% / tmpfs 3.9G 4.0K 3.9G 1% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 511M 5.2M 506M 2% /boot/efi /dev/sdb1 30G 24G 4.3G 85% /tempsdb /dev/loop0 708M 708M 0 100% /tempsdb/iso tmpfs 798M 0 798M 0% /run/user/0
Obviously, the bigger the partition the more disk space will be made free relative to the overall size, with modern disks 256GB+ you can go as low as 0.01 fairly safely when selecting a percentage however do not forget this is a protection mechanism so never go to 0
General Housekeeping #
Sometimes some good old-fashioned housekeeping is all that is needed, after many months or years of running and upgrading your operating system you end up with a lot of clutter, unused packages, and outdated kernels, removing these can free up a surprising amount of disk space.
Debian/Ubuntu
To remove all old packages in the apt cache you can run:
apt-get clean
To remove old kernels you can run:
apt-get autoremove --purge
To remove now unused out of date packages that were installed as dependencies to previous versions you can run:
apt-get autoremove
CentOS/RHEL
To clean up the old yum caches first install the ‘yum-utils’ package then you can run:
yum clean all
To remove old kernels (except the last 2) you can run:
package-cleanup --oldkernels --count=2
Remove Old Log Files #
After many months or years, you will have quite the collection of log files that you will probably never, ever use or need, and if you do, you can probably retrieve them from your backups.
Did you know that Clouvider provides 50GB of free cloud backup space with Dedicated servers through Acronis Cyber Backup check it out here: acronis-cyber-cloud-backup
So it is probably safe to get rid of anything more than 30 days old, we can do this with a command string that searches your log files for anything older than 30 days which is larger than 1MB in size, and automatically remove it.
The following will remove (truncate) anything in /var/log (and subdirectories) with the .log extension
find /var/log -type f -name '*.log' -mtime +30 -exec truncate --size 0 "{}" \;
However, some old log files will be automatically compressed into .gz files so in order to also remove those run:
find /var/log -type f -name '*.gz' -mtime +30 -exec truncate --size 0 "{}" \;