Clean up Ubuntu to increase free hard drive space

After a 2 year hiatus, I started using Ubuntu recently. Of course, being on a laptop means that disk space was limited, so I started with a 25 GB partition.

Soon after installing Ubuntu, I regretted that decision, because after the base install+apps+work and media files, I had a serious space crunch. Resizing the partition is sometimes risky – I learnt it the hard way after being left with an unbootable install in the past.

Windows has built-in tools to clean up stale and unnecessary files, and there is also CCleaner to clean up files (also see CCleaner Enhancer).

Ubuntu does not have a one-click solution, but with a little effort, you can cleanup the install. I tried to trim the Ubuntu install as much as possible, removing unused files, packages, apps, cleaning up tempoorary files, apt files and all sorts of other useless data hogs.

I will update this as I find other tricks to trim down Ubuntu and reclaim much needed disk space, at least for those of us with smaller drives and/or laptops.

Downloaded packages
Ubuntu, and Linux in general, has such a wide variety of software for any given task, that picking one can be difficult. My thinking is, why pick one when you can try them all! I download dozens of apps only to try them out and uninstall later. This is a great way to pick the apps best suited to you.

When you download apps, the packages (in Ubuntu, they are .deb files) you pull down via apt-get, Software center or Synaptic are stored in /var/cache/apt/archives. This is usually not removed by Ubuntu. The reason they are stored there is for reinstalls. However, re-downloading them is a better idea in case of reinstall, as you can not only save disk space, but also get any updated version at the time of install.

The following command shows you how much space is consumed by the apt archives:

du -h /var/cache/apt/

There are a few options for removing these packages:
The nuclear option, which I use, removes all packages from /var/cache/apt/archives and /var/cache/apt/archives/partial is
sudo apt-get clean

A more conservative approach is to remove expired packages from /var/cache/apt/archives and /var/cache/apt/archives/partial. The command is
sudo apt-get autoclean

Some packages are installed as dependencies for other apps. If you have removed those apps later, the dependencies remain and are no longer required. To remove them, use
sudo apt-get autoremove

To automate cleaning up files after install, go to System > Administration > Synaptic Package Manager > Settings > Preferences > Files > Delete downloaded packages after installation.

Delete Logs
Log files are found at /var/log. Over time, they can build up in number and file size, and take up a lot of disk space. The quick fix is to delete log files. However, many files will be locked, or otherwise regenerated immediately after deletion. You should also find the cause of the log files and fix them. Reading log files and fixing issues needs long articles of its own, so I can’t elaborate them here, but in general, finding which app/process caused the issue, then searching online for a fix should help you.

Empty Trash
Emptying trash is a one click process in Windows and Mac. In Ubuntu, things are different – there are several locations for deleted files, depending on user level, among other things.

Running the command below should help find trash folders:
sudo find / -type d -name ‘*Trash*’

Now, open a Nautilus window with admin rights (sudo nautilus) and delete the folders with shift-delete, or alternatively, use the rm command from a terminal.

Clear lost+found folders
The lost+found folder on each partition is used by the system to store files marked as corrupted after an fsck check. The command below locates all the lost+found folders:
sudo find / -name “lost+found”

Again, delete folders with a Nautilus window with admin rights, or rm command from a terminal.

Remove unused system packages
While fans and critics may disagree with this, the reality is that Ubuntu has grown bloated over the years, while still lacking many essential tools. We have personal preferences for apps, too. For example, I prefer VLC over all the preinstalled video players, and Amarok for audio. I found this command somewhere a long time ago, to remove a bunch of unused packages and apps in one go, and have since updated and tweaked it for use. Customize it and run as you find useful.

sudo apt-get autoremove thunderbird pidgin-data brasero totem seahorse seahorse-plugins gnome-games-data openoffice.org-help-en-us ttf-arphic-uming libsane ttf-kochi-gothic vinagre ttf-thai-tlwg listen bluez catfish

Compress usr directory
Ubuntu has a squashfs utility to compress the /usr directory. Compressing it should save a GB of space, and also reduce disk head movement and improve battery life. UbuntuForums has a nice guide on compressing /usr.

Remove residual configuration files
Residual Config files are packages left behind after packages are uninstalled. To remove them, navigate to System > Administration > Synaptic Package Manager.

Click Status, then “Residual config”. If you have any packages listed there, mark them for complete removal and hit Apply. If Residual config is not shown, then you do not have any packages to remove.

System tools to clean up Ubuntu
Here are a bunch of system tools and utilities, some pre-installed, and others from the Software Center/apt, which can help in cleaning up useless files:
Bleachbit cleans up system cache and other files.
Computer Janitor can show you unused system library files and resources. Find it under System > Administration.
Synaptic has a tool called cruft, that does something similar.
gtkorphan is another tool to eliminate orphaned libraries. Install it via Software Center or Apt. It also has a commandline counterpart, called deborphan.
localepurge cleans up language packages on your system. It can also prevent future installs of language packs.

Search for and install Bleachbit in Software Center. It combines several important files and folder cleanup – including system cache and unused system files, packages and languages, browser files etc.

Remove orphaned packages with deborphan. Search and install from Synaptic. It finds packages that are not depended upon by other apps. Once installation of deborphan is complete, open Terminal and execute the following code:

sudo deborphan | xargs sudo apt-get -y remove --purge

You will need to enter your account password when asked (tip for beginners: the cursor does not move even when you type, ignore it, type and hit enter). A bunch of files will be deleted and its details will be listed on Terminal.

Delete unused locale data with localepurge. Search for it on Synaptic and install. It removes unneeded locale files and localized man pages. The cleanup will be executed upon completion of install. When selecting languages, check to keep only those you use – for me, it would be English (en).

All the above tools deal with system files and libraries, so you should be extra-careful, and read any warnings or additional info provided by them.

Of course, the disk space reclaimed by these tools will be minimal, considering the nature of files and folders it helps clean up.

There are many other methods, like using tune2fs to reduce or remove space reserved by Ubuntu, but many of them provide diminishing returns, are too complex and risky that I prefer not to use them. Unless you are terribly deprived of hard drive space, and the above steps did not help at all, I suggest not trying them.

Leave a Reply

© Techzilo 2020 All Rights Reserved.

preloader