How to Analyze and Optimize Disk Space Usage in Linux

Running out of disk space is a common issue on Linux systems — whether it’s a server, desktop, or embedded device. Low disk space can cause failed updates, service crashes, or even data loss.

In this guide, you’ll learn how to analyze what’s taking up space on your system and how to safely free it up using built-in Linux tools and a few handy utilities.

1. Check Overall Disk Usage with df

Start by checking which partitions are running low on space:

df -h
  • -h makes sizes human-readable (e.g., GB, MB)
  • Focus on the Use% column to find heavily used partitions

2. Find Large Directories with du

To identify which directories consume the most space:

du -sh * | sort -hr | head -n 10

Explanation:

  • -s shows total size per item
  • -h for human-readable output
  • sort -hr sorts by size in descending order

This is useful inside directories like /var, /home, or /.

3. Locate Large Files

Use find to search for large files that may be clogging up disk space:

find / -type f -size +100M 2>/dev/null

Or scan your home directory for files larger than 500MB:

find ~/ -type f -size +500M

4. Use ncdu for Interactive Terminal Analysis

ncdu is a powerful, terminal-based interface for analyzing disk usage:

sudo apt install ncdu   # On Debian/Ubuntu
sudo ncdu /

You can browse directories interactively and even delete files directly from the interface.

5. Clean Temporary and Log Files

Start with removing unnecessary temporary files:

sudo rm -rf /tmp/*

Clean up system logs:

sudo journalctl --vacuum-time=7d

Delete compressed or old logs manually:

sudo rm -rf /var/log/*.gz /var/log/*.1

6. Clear Package Manager Cache

  • APT (Debian/Ubuntu):
sudo apt clean
  • DNF (Fedora):
sudo dnf clean all
  • Pacman (Arch):
sudo pacman -Sc

These commands can free up hundreds of megabytes or more, depending on how often you update your system.

7. Automate with Cron and Scripts

To keep your system clean without manual effort, you can use cron to schedule recurring tasks such as log cleanup or disk usage reporting.

How to Edit Cron Jobs

To edit the crontab for the current user, run:

crontab -e

This will open the cron configuration file in your default text editor (usually nano or vim).

Weekly Log Cleanup

Add this line to schedule log cleanup every Sunday at 3 AM:

0 3 * * 0 /usr/bin/journalctl --vacuum-time=7d

This means:

  • 0 → minute
  • 3 → hour (3 AM)
  • * * 0 → every Sunday

You can replace the command with any other script, like cleaning /tmp, sending usage reports, etc.

Verify Scheduled Jobs

To list your current cron jobs:

crontab -l

Conclusion: 5 Tips for Preventing Disk Space Issues

  1. Set journal log retention limits (journalctl)
  2. Run ncdu monthly to stay ahead of large files
  3. Clear package cache after system upgrades
  4. Don’t forget to clean /tmp and /var/log
  5. Schedule disk usage checks and automate reporting