How to Free up Disk Space on Ubuntu Linux ?

ยท 814 words ยท 4 minute read

Empty Trash ๐Ÿ”—

Empty the trash can.

empty the trash can

Or use this command to empty the trash.

rm -r ~/.local/share/Trash/info/ && rm -r ~/.local/share/Trash/files/  

Unwanted screenshots ๐Ÿ”—

Delete unwanted screenshots and screencasts if you have any.

Remove Unused Packages ๐Ÿ”—

Use this command

sudo apt-get autoremove
# or
sudo apt autoremove

to remove packages that were automatically installed to satisfy dependencies for some package and are now no longer needed. This includes old Linux kernels that were installed during system upgrades.

Clean the APT Cache ๐Ÿ”—

Ubuntu keeps downloaded update packages in a cache. You can remove these using:

sudo apt autoclean && sudo apt clean
  • autoclean removes unnecessary packages.
  • clean removes all downloaded packages.

Remove Unused Applications ๐Ÿ”—

Uninstall applications you don’t use with:

sudo apt remove <package_name>
# or
sudo apt-get remove <package_name>

Replace <package_name> with the actual application name.

Use the following command to see list of all installed packages, sorted by size. If you see something big and don’t use it - uninstall it.

dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less

Identify Large Files and Folders ๐Ÿ”—

Use tools like:

  • Baobab (GUI) - Analyzes disk usage visually.
  • ncdu (terminal) - Shows folder sizes like Baobab in the terminal.
  • find / -type f -size +1024k - Search for files larger than 1024 KB.

Remove Old Kernels ๐Ÿ”—

You can usually keep the latest kernel and remove older ones. List kernels with:

ls -lh /boot

Remove them one by one (replace <version> with the actual kernel version):

sudo rm -rf /boot/vmlinuz-<version>

unused language packs ๐Ÿ”—

Remove unused language packs with sudo apt install localepurge.

Find and Remove Duplicate Files ๐Ÿ”—

Duplicate files can take up unnecessary space. Tools like FSlint (GUI) or FDUPES (command line) can help identify and remove duplicate files.

Systemd Journal Cleanup ๐Ÿ”—

On systems using systemd, you can free up space by removing old archived journal files with

sudo journalctl --vacuum-size 10M

adjusting the size parameter as needed.

Stay Updated ๐Ÿ”—

Keep your system and packages updated. Sometimes, updating packages can free up space rather than consuming more.

Run Bash script to remove unused snaps ๐Ÿ”—

You can use this script to clean all older versions of snaps from your computer. Save the script to a file, give it execute permission and run it:

Make sure you close all snaps before running this script.

#!/bin/bash
# Removes old revisions of snaps
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done

remove marked for removal but still have configuration files ๐Ÿ”—

This command is designed to purge (completely remove) packages that have been marked for removal but still have configuration files left behind.

dpkg -l | grep ^rc | cut -d ' ' -f3 | xargs sudo apt-get purge -y

clean up Gradle ๐Ÿ”—

Your Gradle Home directory contains wrapper, caches and daemons files. The more projects with diffferent gradle versions the more subdirectories within those three directories. You can delete all three directories. This saved me a cool ~10GB of disk space.

Just use these two commands.

cd ~/.gradle
rm -rf caches daemon wrapper

clean up Android SDK ๐Ÿ”—

Android System images are only used by emulators. If you use a real Android device during development for debugging, you no longer need them, so you can remove them all. This could easily save you 5GB doing this.

clean unwanted android sdk

Logs ๐Ÿ”—

Logs are the logging of events of programs and apps on your Ubuntu Linux. Old logs are obsolete. This command will delete all logs everywhere in your Ubuntu Linux OS.

find ~/ -type f 2> /dev/null | grep '\.log$' | sed 's/[[:space:]]/\\\ /g' | xargs rm

remove unused docker images ๐Ÿ”—

If you are using docker for your local development, use this command docker image prune to delete obsolete Docker images downloaded on your machine.

docker image prune

That saved me +2GB of storage.

other helpful commands ๐Ÿ”—

Get all directories sorted by size.

du -hd 1 2> /dev/null | sort -hr

Get all files & directories, sorted by size.

ls -AhlS

Get all files which have sizes more than 500MB.

find / -type f -size +500M 2> /dev/null | xargs du -h 2> /dev/null

Get all error log files ending with .err extension.

find / -type f 2> /dev/null | grep '\.err$' | xargs du -h 2> /dev/null

You can empty out the files instead of deleting them by this command.

echo "" > /var/log/example_file.log

But if you still want to delete a file, use this command.

rm /var/log/example_file.log

I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , Facebook , Telegram and GitHub .

Share: