← back

Free up space on Ubuntu: The Easy Way

This is a simple, step-by-step guide on using the terminal to remove garbage files from Ubuntu to free up space. This should work on any Ubuntu-based Linux distribution as well as Linux Mint.

Additionally, I’ve included a shell script at the end that should make performing a disk clean on your Ubuntu computer as simple as copying, pasting, saving, and running two commands.

You can follow along in 2 ways:

  1. Step-by-step guide
  2. One-shot: Do It All

1. Step-by-step guide

Good riddance of unwanted packages

If you’ve been using Ubuntu, you have undoubtedly used the apt-get commands. It helps in installing packages and libraries.

The following apt-get command eliminates packages that were installed as program dependencies but were left in place when the program they were dependent on was deleted.

sudo apt-get autoremove

Cleaning APT cache

As APT carries out the installation, upgradation, and removal of packages, it keeps a cache of DEB packages. If this cache is left unchecked, it can grow quite a bit.

To check the current cache size, use the du command:

sudo du -sh /var/cache/apt

This apt-get command will purge the APT cache folder:

sudo apt-get clean

You can check with the du command as to how much difference it makes.

Clearing the System Journal Logs

Cleaning out the Linux logs can make a significant difference. Yes, these are essential to investigate what’s going on in your system, but over time these logs end up eating up a lot of system space.

You can check how much disk space the system journal logs are taking using:

journalctl --disk-usage

To clean out these system journal logs, carry out:

sudo journalctl --vacuum-time=3d

If you are having the itch to go check what’s the change in disk usage, feel free to do so using the previous journalctl --disk-usage command

Cleaning out the thumbnails

This ain’t much, but it’s honest work.

rm -rf ~/.cache/thumbnails/*

This might not be significant, but it’s something.

Removing older versions of SNAP applications

SNAP packages are usually bigger in size which in turn ends up eating a lot of disk space, and to add to the ginormous package sizes, it also ends up storing up to two older versions of the packages.

I don’t use SNAP as my package installer for most applications, but if you do, this should help out:

  1. Close off all your SNAPS (SNAP applications)
  2. Open a text editor
  3. Copy-paste this code from Alan Pope from the Snapcraft team at Canonical (the company that produces Ubuntu)
#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done
  1. Save this shell file in a decent place with the extension .sh, for example .sh. For my machine, I’ve created a scripts folder in the home directory and placed it there.

  2. Open the folder location of this file or navigate to it using the cd command on your terminal. At the end, your terminal should be open where this file is saved.

  3. Give this file execution permission by running chmod

chmod +x <filename>.sh
  1. Then you should be able to run this file using sudo to clear out the junk that SNAP ends up creating
sudo ./<filename>.sh
...

If you’re too lazy, like I am. The next method is to execute all these commands in one go by writing out a shell script, like you just did.

2. One-shot: Do It All

Every time you have the urge to clear up space on Ubuntu, all this approach does is combine the earlier methods into a bundle that is served in a single line to the terminal.

Open up your text editor.

Copy-paste this and save it at a decent location with the file extension of .sh as this is a shell script.

#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS

set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
    snap remove "$snapname" --revision="$revision"
done

# Vacuum old journal logs older than 3 days
journalctl --vacuum-time=3d

# Remove unnecessary packages
apt-get autoremove -y

# Clean up APT cache
apt-get clean

# Remove cached thumbnails
rm -rf ~/.cache/thumbnails/*
  1. Use the cd command on your terminal to get to the file’s location, or open the folder location and then right-click in a free space to Open in Terminal. At the end, your terminal should be open where this file is saved.

  2. Give this file execution rights

chmod +x <filename>.sh
  1. Run this file using sudo
sudo ./<filename>.sh
...

Yup, that’s it

I hope this simple and easy guide helped out in freeing up space on your Ubuntu or Ubuntu-like machine.