Ubuntu

How to Free Up Hard Drive Space in Ubuntu

Manually hunting and cleaning up large files and unused apps can be tiresome. These apps and files may take a huge space on your hard drive reducing its overall capacity to store large data. This article will list a few methods that can be used to free up disk space.

Empty Trash Bin

Almost all file managers in Linux distributions send deleted files to the trash bin by default. You should keep an eye on the trash bin and clean it up regularly from the file manager, especially when disk space is running low. Some Linux distributions support scheduled cleanup of trash bin at regular intervals, which can be used for automatic deletion of files from the disk. In desktop environments based on GNOME shell, you can find cleanup settings in the system settings app.

If you prefer command line, trash can be emptied by running the following commands in succession:

$ cd $HOME/.local/share/Trash/files/
$ rm -rfi *

Autoremove Installed Packages No Longer in Use

Ubuntu’s apt package manager comes with a handy command line option simply called “autoremove”. It removes unused packages and old kernels that are installed on the system but are safe to remove as no other package is dependent on them. Use the following command to autoremove packages:

$ sudo apt --purge autoremove

The “–purge” switch ensures that the residual configuration files are also removed along with the packages (excluding configuration files located in the home folder).

Cleanup Packages

Along with the “autoremove” command, Ubuntu’s apt package manager ships with a few more commands that are useful for cleaning up old files. These commands are “clean” and “autoclean”. The “clean” command removes the local cache of fully or partially downloaded “.deb” packages used in the past for a system update or in fresh installation of new applications. The autoclean command does the same with a minor difference. It only removes those packages that are obsolete and no longer available on official Ubuntu servers. The “clean” command can remove more files than “autoclean”. These commands are as follows:

$ sudo apt clean
$ sudo apt autoclean

Disk Usage Analyzer

“Disk usage analyzer” is a graphical app shipped with GNOME based desktop environments. This app does a deep analysis of all files and folders on your drive and sorts them by size. Using the graphical interface, you can identify which files and folders are taking a lot of space and then manually remove them at your own discretion.

Find

The “find” command is available in most Linux distributions by default. It can be used for searching files located in a specific directory. Using find command, it is also possible to check the size of files listed in search results. You can then make a decision to delete them or not. The command below will list files larger than 1024MB in the home directory.

$ sudo find $HOME -type f -size +1024M -exec ls -sh {} +

For reference, below is the output of the above command on my system.

2.3G /home/nit/Downloads/focal-desktop-amd64.iso
2.1G /home/nit/Downloads/focal-desktop-amd64.iso.zs-old

Deborphan

Deborphan is a useful utility that lists packages that aren’t used as dependencies for other packages. Since no other packages are dependent on them, they can be safely removed on user’s discretion. To install deborphan on Ubuntu, run the command below:

$ sudo apt install deborphan

To see a list of orphaned packages, run the command below:

$ deborphan

To list all orphaned packages and remove them at same time, run the command below:

$ deborphan | xargs sudo apt --purge remove

Dpigs

Dpigs is a command line app that displays a list of installed debian packages taking the largest space on the hard drive. To install dpigs on Ubuntu, run the command below:

$ sudo apt install debian-goodies

Now run the command below to see a list of 20 packages that occupy largest space on your hard drive:

$ dpigs -H -n 20

For reference, below is the output of the above command on my system.

Conclusion

These are a few methods you can use to remove obsolete packages and unused files to free up some space on your hard drive. As a precaution, avoid using the commands above on the root folder directly as it may lead to system breakage if wrong files are accidentally removed.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.