Linux

Managing Disk Space in Linux: A Comprehensive Guide

This guide will help you diagnose disk space usage and safely clean your Linux system of unnecessary data, returning gigabytes of free space.

Updated at February 16, 2026
10-15 min
Medium
FixPedia Team
Применимо к:Ubuntu 20.04+Debian 10+CentOS 7+Fedora 35+

Introduction / Why This Is Needed

Disk space on Linux servers and workstations often runs out due to the accumulation of temporary files, package caches, old logs, and unnecessary packages. This guide will help you quickly diagnose which data occupies the most space and safely free up gigabytes without losing important information. You will learn how to use standard utilities (df, du, find) and package managers to keep your system clean.

Prerequisites / Preparation

Before you begin, ensure that:

  • You have access to a Linux terminal with sudo (administrator) privileges to execute removal and analysis commands on system directories.
  • Basic utilities are installed: df, du, find, sort, grep. They are available in any standard Linux distribution.
  • For easier analysis, it is recommended to install ncdu (sudo apt install ncdu or sudo dnf install ncdu), but this is optional.
  • Caution: Do not delete files in system directories (/bin, /sbin, /usr, /etc) unless you are certain of their purpose.

Step-by-Step Guide

Step 1: Check Current Disk Usage

First, determine which disk partitions are full. The df (disk free) command shows the usage of each mounted partition.

df -h

Flags:

  • -h — human-readable, displays sizes in MB, GB.
  • Output: Filesystem Size Used Avail Use% Mounted on. Pay attention to Use% — if the value is close to 100%, this is critical.

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   48G  2.0G  96% /

In this case, the root partition / is 96% full. Further actions will focus on it.

Step 2: Find the Largest Directories

Now you need to find which directories occupy the most space. Start from the root /.

sudo du -h --max-depth=1 / | sort -rh | head -n 20

Explanation:

  • sudo — required to access some system directories.
  • du -h — calculates disk usage by directories.
  • --max-depth=1 — shows only the first level of subdirectories (only root subdirectories).
  • sort -rh — sorts by descending size (human-readable numeric).
  • head -n 20 — outputs the top 20 largest.

If du runs slowly on a large partition, use ncdu (an interactive tool):

sudo ncdu /

It allows you to navigate through directories and see sizes in real time.

Step 3: Clean Package Manager Cache

Package managers store downloaded package archives in a cache. Over the years, they can accumulate several gigabytes.

For Ubuntu/Debian (apt):

sudo apt clean

Deletes all files from /var/cache/apt/archives. Safe, as packages are already installed.

For Fedora/CentOS/RHEL (dnf/yum):

sudo dnf clean all

or for older versions:

sudo yum clean all

Step 4: Remove Old Logs

Logs in /var/log can grow large, especially if there are system errors. Clean up old or compressed logs.

Caution: Do not delete files that are actively being used (e.g., syslog, auth.log without compression). It is better to delete only archives (*.gz, *.old) and logs older than a certain period.

# Delete compressed logs older than 30 days
sudo find /var/log -type f -name "*.gz" -mtime +30 -delete

# Delete regular (uncompressed) logs older than 7 days (use with caution!)
sudo find /var/log -type f -name "*.log" -mtime +7 -delete

Also check the size of specific logs:

sudo du -sh /var/log/*

If you see huge files (e.g., kern.log), you might need to configure logrotate (usually already present).

Step 5: Clean Temporary Files

Temporary directories /tmp and /var/tmp often contain unnecessary data. Clean them, but ensure no important files are present.

# Clean /tmp
sudo rm -rf /tmp/*

# Clean /var/tmp
sudo rm -rf /var/tmp/*

Also check /var/cache — application caches may be there:

sudo du -sh /var/cache/*

If you see large directories (e.g., man, apt, yum), they can be cleaned similarly to Step 3.

Step 6: Remove Unnecessary Packages and Dependencies

Over time, packages installed as dependencies but no longer needed accumulate. Remove them.

For Ubuntu/Debian:

# Show packages that can be removed (dependencies)
sudo apt autoremove --purge

# Also check the list of installed packages and remove unnecessary ones manually:
apt list --installed | less
# Then: sudo apt remove package-name

For Fedora/CentOS:

sudo dnf autoremove

Important: Do not remove packages manually unless you are sure they are not required by the system. The autoremove command is safe.

Verify the Result

After completing all steps, check disk usage again:

df -h

Compare Used and Avail values with the initial ones. You should see an increase in free space (Avail). You can also repeat the directory analysis (Step 2) to confirm.

Possible Issues

  1. Access Denied Error
    • Solution: use sudo for commands requiring administrator rights. Do not run rm -rf without understanding what you are deleting.
  2. Space Not Freed After Deleting Files
    • Cause: a process still holds an open file descriptor to the deleted file.
    • Solution: find the process: sudo lsof | grep '(deleted)'. Then restart the corresponding process (e.g., a service) or terminate it.
  3. System Became Unstable After Removing Packages
    • Cause: a critical package was accidentally removed.
    • Solution: reinstall the package via sudo apt install package-name (or equivalent). If you have a backup of the package list, you can restore the system.
  4. Critical Logs Were Deleted
    • Solution: if you deleted an active log, restart the service that writes to it (e.g., sudo systemctl restart rsyslog) to create a new file. Old data may be lost.
  5. Not Enough Space Even After Cleanup
    • There may be large files not accounted for (e.g., database dumps, virtual machines). Check user home directories and application directories (/opt, /var/lib/docker if Docker is used).

Recommendation: Regularly clean package caches and logs (once a month) to maintain system health.

F.A.Q.

How to quickly check which directories take up the most space?
Is it safe to manually delete files in /tmp?
What to do if space isn't freed after deleting large files?
How to automate cleanup so space doesn't run out again?

Hints

Check current disk usage
Find the largest directories
Clean package manager cache
Remove old logs
Clean temporary files
Remove unnecessary packages and dependencies
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community