How to save space on Linux server

Simon Bennett
Simon Bennett ยท Jan 13, 2021
How to save space on Linux server Artwork

If you are a system administrator and responsible for managing server then it is essential to manage the disk space on your server. Otherwise, you will get the error "the server is running out of disk space". If you have a Tera Bytes of storage then you don't need to clean up your server to free some disk space. But if you have limited disk space then freeing up disk space regularly will be necessary for you.

There are several ways to free disk space on your server. In this tutorial, we will explain how to free disk space on your server with examples.

Requirements

  • A system running Ubuntu operating system.

  • A root password is setup in your system.

Check Free Disk Space

Before starting, it is important to know your current disk space usage and free disk space available in your server. You can run "df -h" command to find the free and used disk space in your system.

Open your terminal and run the command as shown below:

df -h

You should get the following output:

Filesystem Size Used Avail Use% Mounted on /dev/sda1 275G 33G 228G 13% / none 4.0K 0 4.0K 0% /sys/fs/cgroup udev 1.9G 4.0K 1.9G 1% /dev tmpfs 384M 2.3M 382M 1% /run none 5.0M 0 5.0M 0% /run/lock none 1.9G 162M 1.8G 9% /run/shm none 100M 48K 100M 1% /run/user /dev/sda5 179G 36G 135G 21% /Data

A brief explanation of each column is shown below:

  • Filesystem : It shows the disk partition on your system. As per above output, /dev/sda11 and /dev/sda5 is the partition of your hard disk.

  • Size : It shows the total size of each partition. As per above output, total size of the partition /dev/sda1 is 275G and /dev/sda5 is 179G.

  • Used : It shows the used space of each partition. As per above output, /dev/sda1 using 33G of space while /dev/sda5 using 36G of space.

  • Avail : It shows the total available or free space on each partition. /dev/sda1 has 228G of free space and /dev/sda5 has 135G of free space.

At this point, you have enough idea of free and used disk space on your system. Now, you can proceed to free the disk space.

Remove the Packages that are no longer required

When you install some package on Linux it will also install libraries and dependent packages to your system. If you remove the installed package, it will only remove the single package. All the packages that are automatically installed are not removed and they are useless in the system.

So it is always recommended to remove the unused packages from your system to free the disk space.

You can remove them by running the following command:

apt-get autoremove -y

You shoud see the following output:

Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: libntdb1 python-ntdb 0 upgraded, 0 newly installed, 2 to remove and 459 not upgraded. 1 not fully installed or removed. After this operation, 188 kB disk space will be freed. Do you want to continue? [Y/n]

The above command will also remove the unused kernel from your system.

Clean the APT Cache

When you install any package using the apt command it will download the .deb file and save it inside /var/cache/apt/archives directory. Over time, these packages can grow large and hold a lot of packages you don't need.

So it is recommended to remove them regularly to free the disk space.

You can see the size of the APT cache with the following command:

du -hs /var/cache/apt/

You should see the size of the cache directory in the following output:

163M /var/cache/apt/

Now, you can remove all the packages kept in the APT cache by running the following command:

apt-get clean -y

Clear Systemd Journal Logs

By default, every system has a logging mechanism. This will helps you to troubleshoot system error or any application related errors. Over the time these log files will kept a large amount of disk space. So it is also recommended to clean up those log files regularly to save the disk space.

You can check the log size by running the following command:

journalctl --disk-usage

You should see the following output:

Journals take up 90M on disk.

You can now clean the logs that are older than a 2 days by running the following command:

rm -rf /run/log/journal/*

Remove Duplicate Files

You can also save your disk space by finding and removing duplicate files from your system. You can use FDUPES command-line tool to find the duplicate files in Linux.

First, install the FDUPES package with the following command:

apt-get install fdupes -y

Once installed, you can find the duplicate files by specifying specific directory.

For example, find all duplicate files inside /var/www/ directory, run the following command:

fdupes -r /var/www/

You should see the following output:

/var/www/html/file2.txt /var/www/html/file3.txt /var/www/html/file1.txt /var/www/file2.txt /var/www/file3.txt /var/www/file1.txt

Now, you can delete all duplicate files with the following command:

fdupes -d /var/www/

You will be asked to delete all files as shown below:

[1] /var/www/file2.txt [2] /var/www/file3.txt [3] /var/www/file1.txt Set 1 of 1, preserve files [1 - 3, all]: all [+] /var/www/file2.txt [+] /var/www/file3.txt [+] /var/www/file1.txt

Uninstall Unwanted Application

There is a good chance you have software installed on your server that you never use and don't require. So it is a good idea to find and remove all those unwanted applications from your server.

You can run the following command to uninstall the packages from your system:

apt-get remove app-name1 app-name2 -y

Once all the applications are removed, run the following command to remove all dependencies that are no longer required:

apt-get autoremove -y

Conclusion

In the above guide, you learned how to free up disk size with several ways. I hope this will helps you to save space on your Linux server.