Last Updated on May 22, 2022
Command-line
Let’s explore some key ways to clean your system from the command line.
Clean the apt cache
The command apt provides a commandline interface for Ubuntu’s package management system.
Ubuntu keeps .deb packages it downloads and stores them in a cache on your disk. A cache is a temporary storage system used to speed up frequent data access when the usual access method is expensive (performance-wise). Generally the advantages of this approach outweighs its disadvantages. But this cache can consume hundreds of MBs of storage.
You can identify how much storage is consumed with the command:
$ sudo du -ch /var/cache/apt/archives/
We can clean the cache with the command:
$ sudo apt clean
The command clears the local repository of retrieved package files that are left in /var/cache. The directories it cleans out are /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.
As shown in the image above, you don’t receive any notification of how much space has been reclaimed.
Remove orphaned packages
We recommend removing orphaned packages. The autoremove option removes packages that were automatically installed because some other package required them but, with those other packages removed, they are no longer needed. Sometimes, an upgrade will suggest that you run this command. The packages to be removed are often called ‘unused dependencies’.
$ sudo apt autoremove
You can combine the two shell commands using the && operator.
$ sudo apt apt clean && sudo apt autoremove
Uninstall unused software
With a huge range of Ubuntu packages and snaps available from the Ubuntu Software app, it’s inevitable you’ll install lots of open source software. For example, you might install half a dozen web browsers before you decide which one you prefer.
Once you’ve identified your favorite software, it’s advisable to uninstall programs you’ll probably never use again. Unused apps are just taking up disk space and may even start processes that run in the background. For example, let’s say you want to uninstall Midori (a web browser), type the command:
$ sudo apt remove midori
Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages will be REMOVED midori 0 to upgrade, 0 to newly install, 1 to remove and 0 not to upgrade. After this operation, 4,209 kB disk space will be freed. Do you want to continue? [Y/n]
As you can see, we’ll save approximately 4MB of disk space by uninstalling Midori.
And don’t forget to remove programs installed by snaps. You can list out the snaps installed with the command:
$ snap list
and remove a specific snap with the command:
$ sudo snap remove name_of_package
Monitor Disk Usage
du, stands for disk usage, remains a popular tool to estimate file space usage. It’s part of coreutils, a package of GNU software containing implementations for many of the basic tools which are used on Unix-like operating systems.
If you find du too basic, we recommend dust, a sublime alternative to du which offers better presentation of information.
On a vanilla installation of Ubuntu 21.04, we first need to install cargo. Cargo is the Rust package manager. Cargo downloads your Rust package’s dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io, the Rust community’s package registry.
cargo is available as a regular Ubuntu package or a snap. We chose to install the former:
$ sudo apt install cargo
Now we can proceed and install dust using cargo, with the command:
$ cargo install du-dust
By default du-dust is installed to ~/.cargo/bin
That directory isn’t in our PATH. PATH is an environment variable specifying a set of directories where executable programs are located. Let’s permanently add ~/.cargo/bin to our PATH. Fire up nano or whatever text editor you prefer and edit the .bashrc file.
$ nano ~/.bashrc
At the end of the file, add the line:
export PATH=$PATH:/home/user_name/.cargo/bin
Replace user_name with your username.
Save the file and exit. At the shell, enter the command:
$ source ~/.bashrc
Instead of the source command, you can log out and log into a new shell.
We are now ready to run dust.
Pruning from the command-line
We end this article with a summary of other generic actions you can do:
- Clean up /tmp and /var/tmp;
- Remove files in /var/cache;
- Remove log files that are not autorotated;
- Remove locked handles from files that may have already been deleted;
- Disable daemons that aren’t needed. To stop a daemon issue the command:
$ /etc/init.d/daemon-name stop
; - Remove old kernels. We can list all installed Linux kernels with the command:
$ dpkg --list | grep -i -E --color 'linux-image|linux-kernel' | grep '^ii'
; - Delete unused user accounts (
$ sudo deluser --remove-home userName
); - Delete broken symlinks (
$ find . -xtype l -delete
).Pages in this article:
Page 1 – Introduction / Stacer
Page 2 – Disk Usage
Page 3 – Command-line
All articles in this series:
du is all I use for disk usage