Your submission was sent successfully! Close

You have successfully unsubscribed! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates about Ubuntu and upcoming events where you can meet our team.Close

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

Igor Ljubuncic

on 11 February 2021

This article is more than 3 years old.


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.

Ubuntu desktop

Learn how the Ubuntu desktop operating system powers millions of PCs and laptops around the world.

Newsletter signup

Get the latest Ubuntu news and updates in your inbox.

By submitting this form, I confirm that I have read and agree to Canonical's Privacy Policy.

Related posts

Behind open DORS – Conference organizers share their thoughts on Canonical, Ubuntu, snaps, and open-source

A Linux conference almost as old as Linux itself. In mid-May, DORS/CLUC hosted its 29th event at the Faculty of Electrical Engineering and Computing in...

Creating Snaps on Ubuntu Touch

This article was written in collaboration with Alfred E. Neumayer of the UBports Project. Tablets, phones and current technology’s capabilities are...

Managing software in complex network environments: the Snap Store Proxy

As enterprises grapple with the evolving landscape of security threats, the need to safeguard internal networks from the broader internet is increasingly...