investigation
How to Recover Disk Space on a Full Linux Server
If your Linux server runs out of disk space, here’s a quick, effective process to find and fix the problem.
1. Check Disk Usage
Start by seeing which filesystem is full:
1 | df -h |
If you suspect inode exhaustion (lots of tiny files), check:
1 | df -i |
2. Find Large Directories
Identify which folders use the most space:
1 | sudo du -h -d 1 -x / 2>/dev/null | sort -hr | head -20 |
-xkeeps the scan on the main filesystem.- For an interactive view, try
ncdu:
1 | sudo apt install ncdu |
3. Remove Unneeded Large Files
Look for unusually large files, especially in /usr/local, /var, or /home. Static libraries (.a files) and old build artifacts are common culprits. If you find something you no longer need, remove it:
1 | sudo rm /path/to/large/file |
4. Fix “Phantom” Disk Usage
If df shows more used space than du, you may have deleted files still held open by running processes. Find them with:
1 | sudo lsof +L1 2>/dev/null | awk 'NR==1 || $7+0 > 10000000' |
Restart the process holding the file (e.g., Docker):
1 | sudo systemctl restart docker |
5. Prevent Future Issues (Docker Log Rotation)
Docker logs can silently fill your disk. Set up log rotation in /etc/docker/daemon.json:
1 | { |
Restart Docker:
1 | sudo systemctl restart docker |
Recreate containers to apply the new policy.
Quick Cleanup Commands
1 | docker system df |
This version removes personal anecdotes and focuses on actionable steps, making it easy to follow as a general-purpose tutorial.
Total recovered: ~7GB. Total time: about 20 minutes. Lesson value: priceless.