sodcalculator

10 Tech How-To

Fix Linux Hard Disk Running Out of Space

Guide 10-12285 · Updated · Step by step

If your Linux system is running out of disk space, the fix is to identify what is consuming the storage and then remove or relocate the largest files, clear package caches, and clean up logs and temporary files. This guide walks you through the most effective commands and strategies to reclaim space quickly and safely.

Why Linux Disks Fill Up

Linux systems accumulate files in predictable places. Package managers store downloaded archives, system logs grow over time, and user caches (browser, thumbnail, and application caches) can consume gigabytes. Docker images, virtual machine disks, and database files are also common culprits on servers. Unlike Windows, Linux does not automatically clean old logs or package caches, so they can grow until they fill the disk.

How to Check Disk Usage

Start by seeing how full your disk is with df -h. This shows mounted filesystems, their total size, used space, and available space in human-readable format. The output includes the mount point, so you can see which partition is full—often / or /home.

To find which directories are using the most space, use du -h --max-depth=1 / (run as root). This lists the size of each top-level directory. Then drill down into the largest ones. For example, if /var is huge, run sudo du -h --max-depth=1 /var to see what is inside. Repeat until you locate the largest folders.

For a visual overview, install a disk usage analyzer like ncdu (terminal) or baobab (GUI). These tools make it easier to spot large files and directories.

Clean Package Manager Caches

Package managers store downloaded .deb or .rpm files in a cache. Over time, this can reach several gigabytes.

  • Debian/Ubuntu (apt): Run sudo apt clean to remove all cached .deb files. Use sudo apt autoremove to remove unused dependencies.
  • Fedora/RHEL (dnf): Run sudo dnf clean all to clear the cache.
  • Arch (pacman): Run sudo pacman -Sc to remove old packages from the cache, or sudo pacman -Scc to remove all cached packages.

Remove Old Logs and Journal Files

System logs are stored in /var/log and the systemd journal in /var/log/journal. Logs can grow indefinitely if not rotated. Check the journal size with journalctl --disk-usage. To trim it to a reasonable size (e.g., 100 MB), run:

sudo journalctl --vacuum-size=100M

For traditional log files in /var/log, use sudo find /var/log -type f -name "*.log" -exec truncate -s 0 {} \; to empty them. Be careful not to delete the files themselves, as services expect them to exist.

Find and Remove Large Files

The find command can locate files larger than a certain size. For example, to find files over 100 MB in the home directory:

find /home -type f -size +100M -exec ls -lh {} \;

You can also use du -ah /home | sort -rh | head -20 to list the 20 largest files and directories. Once you identify large files, decide whether to delete them, move them to an external drive, or compress them.

Clean User Caches

User caches are stored in ~/.cache. This is safe to clear, as applications will recreate the cache as needed. To see its size, run du -sh ~/.cache. To delete all contents:

rm -rf ~/.cache/*

Browser caches are often the biggest offenders. In Firefox, you can clear the cache via Settings > Privacy & Security > Cookies and Site Data > Clear Data. In Chrome, go to Settings > Privacy and security > Clear browsing data.

Address Docker and Virtualization Overhead

If you use Docker, images, containers, and volumes can consume significant space. Run docker system df to see usage. Use docker system prune -a to remove unused images, containers, and networks. Be aware that this removes all stopped containers and unused images, which may require re-downloading them later.

Virtual machines store disk images that can be enormous. Check your VM manager (e.g., VirtualBox, KVM) for snapshot files and delete old ones. Also consider using sparse disk images or resizing the virtual disk if it is larger than needed.

Common Problems and Misconceptions

“I deleted files, but df still shows full.� This happens when a process still holds the deleted file open. Use lsof +L1 to list files that are deleted but still open, then restart the process to release them.

“The trash bin is not empty.� Files you delete via a file manager go to ~/.local/share/Trash. Empty it with rm -rf ~/.local/share/Trash/*.

“I don’t have permission to delete files.� Use sudo for system directories, but be careful not to delete critical system files. Always double-check the path.

Frequently Asked Questions

How do I find what is taking up space on Linux?

Use df -h to see which partition is full, then du -h --max-depth=1 / to find the largest directories. Drill down until you locate the biggest files or folders. Tools like ncdu make this easier.

Is it safe to delete /var/cache/apt/archives?

Yes, those are downloaded package files that are no longer needed after installation. Running sudo apt clean removes them safely.

Why does my disk fill up after I delete files?

If a process still has the file open, the space is not freed until the process closes it. Use lsof +L1 to find such files and restart the process.

What is the fastest way to free up space?

Clearing package caches and journal logs often yields the quickest gains. Run sudo apt clean (or your distribution’s equivalent) and sudo journalctl --vacuum-size=100M.

Can I move /home to another drive to free space?

Yes, you can move the /home directory to a larger partition or drive. This involves copying the data, updating /etc/fstab, and rebooting. It is a more advanced operation, so back up your data first.

Is there a command to see the largest files on my system?

Yes, use sudo find / -type f -size +100M -exec ls -lh {} \; to list files larger than 100 MB. You can adjust the size to suit your needs.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →