How to check disk usage and clean up space
Topic: Servers linux
Summary
Find what is using disk space with du and df, locate large files and dirs, and free space by removing logs, caches, and old packages. Use this when the disk is full or you need to prevent full before it happens.
Intent: How-to
Quick answer
- df -h for filesystem usage; du -sh * in a dir to see subdir sizes; du -x / to find large dirs (skip other mounts) or ncdu for interactive.
- Free space: clear old logs (journalctl --vacuum-time, delete /var/log/*.gz), apt/dnf cache (apt clean, dnf clean all), and old kernels if safe.
- Identify large files with find -size or du; move or compress; fix the app or log rotation so the problem does not recur.
Prerequisites
Steps
-
Check filesystem usage
df -h; note which mount is full (e.g. / or /var); use du -sh /var/* to see which subdir under /var is largest.
-
Find large directories and files
du -x / 2>/dev/null | sort -n | tail -20; or install ncdu and run ncdu /; find /var -type f -size +100M -exec ls -lh {} \; to list large files.
-
Clear safe targets
journalctl --vacuum-time 3d; rm /var/log/*.gz; apt clean or dnf clean all; remove old kernels (list with dpkg -l | grep linux-image or rpm -q kernel; keep current and one back).
-
Fix recurrence
Configure logrotate or journald limits; cap app logs; add monitoring/alert when usage exceeds threshold; consider expanding disk or moving data.
Summary
You will find what is using disk space, free space by clearing logs and caches safely, and reduce the chance of it happening again with rotation and monitoring. Use this when the disk is full or usage is high.
Prerequisites
- Shell access; root or sudo for cleaning system dirs.
Steps
Step 1: Check filesystem usage
df -h
du -sh /var/* /home/* 2>/dev/null
Identify the full mount and the largest directories.
Step 2: Find large directories and files
du -x / 2>/dev/null | sort -n | tail -30
find /var -type f -size +50M -exec ls -lh {} \;
Use ncdu for an interactive view if available.
Step 3: Clear safe targets
sudo journalctl --vacuum-time 7d
sudo find /var/log -name "*.gz" -delete
sudo apt clean
sudo dnf clean all
Remove old kernels only if you have tested boot with the current one and keep at least one previous.
Step 4: Fix recurrence
- Configure logrotate for app logs; set journald SystemMaxUse= in /etc/systemd/journald.conf.
- Add a cron or systemd timer to alert when usage > 85%; expand disk or move data if growth is expected.
Verification
- df -h shows increased free space; services still run; logs still written; no critical files removed.
Troubleshooting
Cannot free enough — Identify the single largest consumers (often one log or one DB/file store); truncate or archive and fix the source; consider adding disk.
Deleted something important — Restore from backup if available; otherwise fix config and restart services; document what was removed to avoid repeating.