Free Up Disk Space on Linux

Need to Free Up Disk Space on a Linux Server or Desktop? You’ve come to the right place. Perhaps you have seen our other article about how to check disk space on Linux.

When facing low disk space issues on a Linux system, there are several symptoms that may indicate the problem. Here are some common signs to look out for:

Warning Messages:

Linux systems often generate warning messages when the available disk space is critically low. Keep an eye on system notifications, logs, or terminal messages for alerts regarding low disk space.

Failed Software Updates:

If you notice that software updates or installations are failing, it could be due to insufficient disk space. The package manager might require space to download and install updates.

Slow System Performance:

Low disk space can significantly impact system performance. As the available space decreases, applications and the overall system’s response times may slow.

Application Errors:

Some applications may start throwing errors or crash unexpectedly when they can’t write data due to a lack of disk space. Check application logs for any error messages related to disk space issues.

Unresponsive System:

In extreme cases, a lack of disk space may render the system unresponsive. The operating system may struggle to perform basic functions, leading to freezing or crashing.

Unable to Save Files:

If you can’t save new files or data, it could be a clear indication of insufficient disk space. The operating system needs space to write and store user data.

Difficulty Logging In:

Insufficient disk space can affect the ability to log in, especially if the system requires space for creating temporary files during the login process.

System Warnings in GUI:

Graphical user interfaces (GUIs) in Linux environments often provide visual warnings or alerts when disk space is running low. This could include notifications on the desktop or file manager.

How to Free Up Disk Space on Linux

Step 1 – Work out What Is using the Disk Space

This is often the trickiest part of the entire process. It can be hard to know what is eating up your disk space. Is it a process that has gone rogue, or is it a log file that is spamming verbose? Who knows! But let’s find out how to Free Up Disk Space on Linux

The easiest way to read your hard disk is to use the du command. If you just use the du command, it will list endless directories, which is not very helpful. You could narrow it down by using a specific folder to check, but that only works if you know what folder is filling up your hard drive.

For Example, let’s check my /tmp folder but only list the top 10 biggest folders.

Bash
sudo du -h --max-depth=1 /tmp | sort -rh | head -n 11

Below is my output. You can see the root of /tmp is 270 MB in size, and listed are the biggest 10 folders, sorted by size, largest to smallest.

Bash
270M	/tmp
269M	/tmp/ollama2115724660
744K	/tmp/v8-compile-cache-1000
342K	/tmp/snap-private-tmp
222K	/tmp/pkg-TqDcR8
30K	/tmp/.com.google.Chrome.tGZUvs
18K	/tmp/systemd-private-1f1b7e094b504f18891cf3247dddd4e4-upower.service-uPEGSc
18K	/tmp/systemd-private-1f1b7e094b504f18891cf3247dddd4e4-systemd-timesyncd.service-s4RoAW
18K	/tmp/systemd-private-1f1b7e094b504f18891cf3247dddd4e4-systemd-resolved.service-sAq0tJ
18K	/tmp/systemd-private-1f1b7e094b504f18891cf3247dddd4e4-systemd-oomd.service-GYMie5
18K	/tmp/systemd-private-1f1b7e094b504f18891cf3247dddd4e4-systemd-logind.service-J4DnMA

You can repeat this against the entire filesystem if you want.

Bash
du -h --max-depth=1 / 2>/dev/null | sort -rh | awk '{if ($1 != "0") printf "%-40s%-10s\n", $2, $1}' | head -n 11

This is my output

Bash
/                                       79G       
/home                                   38G       
/snap                                   17G       
/usr                                    13G       
/var                                    8.7G      
/installables                           2.6G      
/boot                                   353M      
/opt                                    321M      
/etc                                    16M       
/run                                    3.1M      
/tmp                                    1.4M 

You can immediately see that my home folder is the biggest folder on my system and a perfect start to Free Up Disk Space on Linux

Step 1.1 – My Favorite Disk Space Commands

Here are some of my favorite disk space commands:

Bash
#List Top Disk-consuming Files:
find / -type f -exec du -h {} + | sort -rh | head -n 10 

#List Top Disk-consuming Directories:
du -h --max-depth=1 /path/to/directory | sort -rh | head -n 11

#Find Large Files in Home Directory:
find ~/ -type f -exec du -h {} + | sort -rh | head -n 10

#Identify Large Files Older Than X Days:
find / -type f -mtime +X -exec du -h {} + | sort -rh | head -n 10

#Find and Delete Large Log Files:
find /var/log -type f -size +100M -exec ls -lh {} + | awk '{print $9, $5}'

Step 2 – Clean Package Cache (Debian / Ubuntu)

If you install packages frequently, or if you have just upgraded/updated your systems. There is a good chance your apt-cache can be cleared down to Free Up Disk Space on Linux

Want to see how much of your cache is being used? You can run this command:

Bash
du -sh /var/cache/apt

You will see output similar to this:

Bash
530M /var/cache/apt

To clear the cache just type:

Bash
sudo apt-get clean

Step 3 – Clear down old Linux Kernel to Free Up Disk Space on Linux

When you update your Operating System, the Kernel is not always cleaned by default. This is because in the event of a system crash, you can always change the kernel used at System Boot. However, over time the number of Kernels will grow, and clearing them down is a great way to Free Up Disk Space on Linux.

Keep in mind that the actual packages might include architecture-specific information (e.g., “linux-image-5.4.0-72-generic” for a specific version). If you want to see only the version numbers, you may need to parse the output further.

Bash
dpkg-query -W -f='${binary:Package}\t${Version}\t${Installed-Size}\n' | grep linux-image | awk '{printf "%s\t%s\t%.2f MB\n", $1, $2, $3/1024}'

This command retrieves information about installed Debian packages, filters for those related to Linux kernel images, and then prints a formatted list containing the package name, version, and installed size in megabytes.

You will see output similar to this:

Elsewhere On TurboGeek:  Easy Way to Install Ansible on Linux

Bash
dpkg-query -W -f='${binary:Package}\t${Version}\t${Installed-Size}\n' | grep linux-image | awk '{printf "%s\t%s\t%.2f MB\n", $1, $2, $3/1024}'
linux-image-5.19.0-32-generic	5.19.0-32.33~22.04.1	11.65 MB
linux-image-5.19.0-46-generic	5.19.0-46.47~22.04.1	11.69 MB
linux-image-6.2.0-26-generic	6.2.0-26.26~22.04.1	13.17 MB
linux-image-6.2.0-31-generic	6.2.0-31.31~22.04.1	13.19 MB
linux-image-6.2.0-32-generic	6.2.0-32.32~22.04.1	13.19 MB
linux-image-6.2.0-33-generic	6.2.0-33.33~22.04.1	13.18 MB
linux-image-6.2.0-34-generic	6.2.0-34.34~22.04.1	13.19 MB
linux-image-6.2.0-35-generic	6.2.0-35.35~22.04.1	13.19 MB
linux-image-6.2.0-36-generic	6.2.0-36.37~22.04.1	13.19 MB
linux-image-6.2.0-37-generic	6.2.0-37.38~22.04.1	13.19 MB
linux-image-6.2.0-39-generic	6.2.0-39.40~22.04.1	13.19 MB
linux-image-6.5.0-14-generic	6.5.0-14.14~22.04.1	13.60 MB
linux-image-6.5.0-15-generic	6.5.0-15.15~22.04.1	13.61 MB
linux-image-generic-hwe-22.04	6.5.0.15.15~22.04.8	0.02 MB

If you are happy to remove the Kernel you can do so by using this command. You may want to keep at least one or two older kernels as a backup in case there are issues with the latest one. Always check that your system is stable after removing old kernels before proceeding further.

Bash
sudo apt-get remove <kernel-package-name>

Step 4 – Truncate Log Files to Free Up Disk Space on Linux

The /var/log directory on a Linux system is where various log files are stored. These log files contain information about system events, processes, and applications.

Examples of common log files within /var/log:

  • syslog: General system log file capturing a variety of events.
  • auth.log or secure: Logs related to authentication and security.
  • messages: General system messages.
  • kern.log: Kernel-related messages.
  • apache2/access.log and apache2/error.log: Apache web server access and error logs.
  • mysql/error.log: MySQL database server error log.
  • Access Permissions: Usually, access to the /var/log directory and its log files is restricted to system administrators (root or users with elevated privileges) to maintain the integrity and security of the logs.

Step 4.1 Find Biggest Five Files

Let’s take a look at the 5 biggest files in my /var/log folder. Run this command:

Bash
du -h --max-depth=1 /var/log 2>/dev/null | sort -rh | awk '{if ($1 != "0") printf "%-40s%-10s\n", $2, $1}' | head -n 5

This is the output I get:

Bash
/var/log/journal                        1.2G          
/var/log/aws-vpn-client                 885K      
/var/log/installer                      610K      
/var/log/apt                            262K      
/var/log/cups                           89K       

4.2 – Truncate Journal – Important Information

Stop! Before you go truncating files, make sure you understand the risk, and only do the truncate if you are certain.

  • /var/log/journal (1.2G):
    • Purpose: This directory contains logs managed by systemd-journald. It stores system logs in a binary format.
    • Usage: Used for centralized and structured logging, allowing easy retrieval and analysis of system events.
  • /var/log/aws-vpn-client (885K):
    • Purpose: Logs related to the AWS VPN client, which is a tool for connecting to AWS Virtual Private Clouds (VPCs).
    • Usage: Records events and activities related to VPN connections.
  • /var/log/installer (610K):
    • Purpose: Logs related to the system installation process.
    • Usage: Helpful for diagnosing issues during the installation of the operating system.
  • /var/log/apt (262K):
    • Purpose: Logs related to package management using APT (Advanced Package Tool).
    • Usage: Captures information about package installations, upgrades, and removals.
  • /var/log/cups (89K):
    • Purpose: Logs for the Common Unix Printing System (CUPS), which manages printing services.
    • Usage: Contains information about print jobs, errors, and printer status.

Ok. I am going to truncate my journal. I don’t need the system information saved within it and clearing 1.2gb of space is a great way to Free Up Disk Space on Linux. First, see what the content of the journal folder is:

Bash
ls -ltr /var/log/journal/*

The output will look something like this:

4.3 – Truncate Journal

I will now Truncate all the journals

Bash
sudo truncate -s 0 /var/log/journal/1301d4d0705a4efca0baa670e1d1df94/*

Now when I list my folder, its truncates the logs and saves me nearly 1GB of data! What a great way to Free Up Disk Space on Linux

Bash
ls -lht /var/log/journal/*
total 10K
-rw-r-----+ 1 root systemd-journal 8.0M Jan 30 14:33 user-1000.journal
-rw-r-----+ 1 root systemd-journal 8.0M Jan 30 14:33 system.journal

That’s all for now. These are 4 of the best ways to Free Up Disk Space on Linux; just be careful.

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

1 Response

  1. 19/04/2024

    […] your looking for detailed disk space procedures, check out our other popular Post about […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate ยป