Step-by-step guide to analyze and clean disk space on Linux systems, including kernel cleanup, cache removal, log management, and disk usage analysis.
Linux Disk Space Cleanup Guide (Command Line)
This guide provides a structured approach to analyzing disk usage and safely reclaiming disk space on Linux systems using command-line tools.
1. Check Disk Usage
Start by reviewing overall disk usage:
df -h
This shows filesystem usage in human-readable format.
2. Identify Large Files and Directories
Top 10 Largest Items from Root
du -a / | sort -n -r | head -n 10
Top 10 Largest Items in /var
du -a /var | sort -n -r | head -n 10
These commands help identify disk-heavy directories, typically logs, cache files, or application data.
3. Clean Old Kernels
RHEL / CentOS / Rocky Linux
rpm -q kernel
package-cleanup --oldkernels --count=1
This removes old kernel versions while keeping the most recent one.
Debian / Ubuntu
dpkg --list | egrep -i 'linux-image|linux-headers'
sudo apt autoremove
This removes unused kernel packages. If residual packages remain (marked "rc"), remove them manually:
sudo apt purge <package-name>
4. Clean Package Manager Cache
RHEL-based Systems
yum clean all
Debian-based Systems
sudo apt clean
sudo apt autoclean
This removes cached packages and frees disk space.
5. Clean System Logs (journalctl)
Reduce the size of systemd journal logs:
sudo journalctl --vacuum-time=10d
This keeps only logs from the last 10 days.
Limit Future Log Growth
Edit the configuration file:
/etc/systemd/journald.conf
Set limits such as:
SystemMaxUse=100M
Then restart the service:
sudo systemctl restart systemd-journald
6. Configure Log Rotation
Log rotation prevents log files from consuming excessive disk space.
Example Configuration
daily
size 20M
rotate 7
compress
notifempty
delaycompress
missingok
Edit Logrotate Configuration
/etc/logrotate.d/freepbx-core
After changes, restart logrotate:
systemctl restart logrotate
7. Best Practices
- Always verify disk usage before and after cleanup
- Keep at least one fallback kernel
- Automate log rotation to prevent future issues
- Monitor /var regularly (logs, caches, application data)
- Be cautious when deleting files in system directories
8. Summary
Effective disk cleanup involves identifying large files, removing unused kernels, clearing package caches, and managing logs. Regular maintenance helps prevent system outages caused by full disks.