How to keep your Linux disk usage nice and tidy and save space

by Igor Ljubuncic on 11 February 2021

Everyone loves a clean, tidy home (hopefully). This also includes your other home – slash home, the Linux home directory. Disk cleanup and management utilities are extremely popular in Windows, but not so much in Linux. This means that users who want to do a bit of housekeeping in their distro may not necessarily have a quick, convenient way to figure out how to get rid of the extra cruft they have accumulated over the years. Let us walk you through the processing of slimming down your home.

Caution, files ahead!

The primary problem with data cleanup is that people can easily get over-enthusiastic and then end up deleting more than they should. Freeing an extra 20 MB of space could lead to frustration and panic if you suddenly discover you purged an important, valuable piece of information. Or you may end up deleting an application configuration file, and will need to redo your work setup all over again.

Therefore, before you do any data removal, it is essential that you have a solid, proven, reliable backup mechanism in place.

  • Back up your personal files – Make sure you have at least one copy of your important data like documents, photos, etc saved in a separate location, preferably a separate device like an external disk or a different computer. You can do this manually or automatically via scheduled runs with tools like rsync.
  • Back up your application settings – The data backup above may already cover this scenario, but you can use a snap like Mackup to create distinct, precise backups of application settings for a whole range of applications. We talked about this in a dedicated tutorial a while back.
  • Have a system snapshot or image – Most people may find this a bit complicated, but it is quite useful to have a full system copy available for restore, should things go bad. A tool like CloneZilla can help you back up and restore your system.

Examine the disk

The next step is to figure out what takes your space and what you can do to potentially remove some of the unwanted stuff. In some cases, the “picture” may be trivial – you have tons of files in your Downloads, you could be using virtualization, so you may have a folder full of virtual machine images, and so forth.

There are numerous ways to achieve this, both on the command line as well as using GUI tools.

  • Ncdu is a handy ncurses-based tool, which you can run in your terminal, and get a usage view of selected folders and locations. If you run the program as root, you can check the entire filesystem. If you run it without root permissions, you can check your home directory. Once the disk is analysed, you will have a simple table view, which you can navigate with arrow keys. Results will be displayed by size, which can help you quickly figure out what takes space. Ncdu is available in most software archives across the different distributions.
  • Alternatively, KDiskFree (kdf) is a graphical tool that allows you to display information about hard disks and other storage devices, including the amount of free space available. It can also mount and unmount storage devices and view them in the file manager. Other tools that do similar things include Filelight or perhaps Disk Usage Analyzer (Baobab) for GNOME.

Manage the disk utilisation

Czkawka is a snap with a tricky name but a very clever idea. It can help you examine your data usage, and then offer suggestions on how to reduce it, including temporary files, music duplicates, similar images, invalid symlinks, empty files, and more. It scans your home directory by default, but you can also manually add other locations – as well as configure the exclusion filters for folders and files. The application also lets you preview different items, so you can check and compare information before you decide something ought to be deleted.

What about the system?

For most people, the system side of things is best left alone. However, you can still do a few safe operations, which let you remove unused content like files downloaded and cached during the update process, and leftover packages that are no longer needed.

The procedure to do this differs from one Linux distribution to another. For instance, in the Debian- and Ubuntu-based systems, which use apt as the package manager, you can remove the cached update files with:

sudo apt clean
sudo apt autoclean
sudo apt autoremove

The first command cleans the packages from /var/cache/apt/archives. The second command removes obsolete packages. The third command removes orphaned packages and dependencies that are no longer needed by the system. However, it does not purge them, so to gain space back, you also need to use the optional –purge flag.

If you use snaps, then you should be aware that the snapd services keeps several revisions of any installed snap on your disk – whenever there’s an update, it will rotate a current version away, and place a new one in its place, a total of three by default. You could change this, which can help reduce the disk space utilisation.

sudo snap set system refresh.retain=2

Furthermore, snapd will automatically create snapshots of removed snaps, so if you intend to restore them, you don’t lose all your snap-associated data. You can also create manual snapshots of your snaps for backup and retention purposes.

You can then remove the packages you do not need, and then also delete any (old) snapshots you don’t need under /var/lib/snapd/snapshots, in addition to tweaking the number of snap revisions you want to keep on your machine.

Conclusion

Overall, Linux will take care of itself, and you can go for years without having to take any specific actions to handle your disk usage. But now and then, you may want to get rid of old leftovers, duplicate files and unused system packages. You need to be able to do this in a smart, non-destructive manner. Luckily, there are quite a few practical tools out there, both command-line and graphical, which can help you analyze the utilization and make the right choices. Do remember to create system and data backups regularly, and then there won’t be any room for mistakes.

If you have any other tips or suggestions on how to manage old data in Linux, we’d like to hear about it. Please join our forum for a discussion, and may your data be safe.

Photo by Gaith Shalan on Unsplash.

Newsletter Signup

Related posts

What is virtualisation? The basics

Virtualisation plays a huge role in almost all of today’s fastest-growing software-based industries. It is the foundation for most cloud computing, the go-to methodology for cross-platform development, and has made its way all the way to ‘the edge’; the eponymous IoT. This article is the first in a series where we explain what virtualisat […]

Want to publish a snap? Here’s a list of dos and don’ts

Technology is a medium that enables us to achieve things in life, ideally in a pleasant way. In the software world, operating systems, programming languages and application frameworks are the tools of the trade. But you need the right tool for the right job. If you’re thinking about writing or porting your code to snaps, […]

Compact and Bijou

Snaps are designed to be self-contained packages of binaries, libraries and other assets. A snap might end up being quite bulky if the primary application it contains has many additional dependencies. This is a by-product of the snap needing to run on any Linux distribution where dependencies cannot always be expected to be installed. Thi […]