Skip to Content

Quick Diagnosis

# Check disk usage df -h # Check directory sizes du -sh /* 2>/dev/null | sort -rh | head -20 # Check large files in the current directory du -sh * | sort -rh | head -20

Clean APT Cache

Downloaded .deb packages are saved in the APT cache by default:

# Check cache size du -sh /var/cache/apt/archives/ # Clear all cache sudo apt clean # Clear only outdated cache sudo apt autoclean # Remove dependencies that are no longer needed sudo apt autoremove

Clean Snap Packages

Snap retains old version snapshots by default, which can consume a lot of space:

# Check snap usage du -sh /var/lib/snapd/snaps/ du -sh ~/snap/ # List all snap versions snap list --all # Remove old snap versions (keep current version) sudo snap set system refresh.retain=2 # Manually clean all disabled old versions snap list --all | awk '/disabled/{print $1, $3}' | \ while read name rev; do sudo snap remove "$name" --revision="$rev" done

Clean System Logs

# Check log usage journalctl --disk-usage # Keep only the last 7 days of logs sudo journalctl --vacuum-time=7d # Limit logs to a maximum of 500MB sudo journalctl --vacuum-size=500M # Permanently limit log size sudo nano /etc/systemd/journald.conf # Set: SystemMaxUse=500M sudo systemctl restart systemd-journald

Clean Old Kernels

# View installed kernels dpkg --list | grep linux-image # View the currently running kernel uname -r # Remove old kernels (do NOT remove the one currently in use!) sudo apt purge linux-image-OLD_VERSION-generic sudo apt autoremove # Automatically clean up old kernels that are no longer needed (purge-old-kernels is deprecated) sudo apt autoremove --purge

Find Large Files

# Find files larger than 100MB sudo find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5 -rh # Use ncdu for interactive analysis (recommended) sudo apt install ncdu sudo ncdu /

ncdu provides an interactive interface for visually inspecting and locating large files and directories.

Clean User Cache

# Browser cache du -sh ~/.cache/ rm -rf ~/.cache/thumbnails/* rm -rf ~/.cache/mozilla/firefox/*/cache2/* rm -rf ~/.cache/google-chrome/Default/Cache/* # Flatpak cache flatpak uninstall --unused

Clean /tmp

# Check /tmp size du -sh /tmp/ # System reboot automatically cleans /tmp # Manually clean temporary files older than 7 days sudo find /tmp -type f -atime +7 -delete

Clean Docker (if installed)

# Check Docker usage docker system df # Clean unused images, containers, networks docker system prune -a # Clean build cache docker builder prune

Expanding Disk Space

LVM Expansion

# Check LVM status sudo lvs sudo vgs # If the VG has remaining space sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

Mount an Additional Hard Drive

# Format the new drive sudo mkfs.ext4 /dev/sdb1 # Mount sudo mkdir -p /mnt/data sudo mount /dev/sdb1 /mnt/data # Write to fstab for persistence echo "UUID=$(blkid -s UUID -o value /dev/sdb1) /mnt/data ext4 defaults 0 2" | sudo tee -a /etc/fstab

Disk Usage Monitoring

Set up disk space alerts to prevent it from filling up again:

# Simple cron check script cat << 'SCRIPT' | sudo tee /usr/local/bin/disk-alert.sh #!/bin/bash USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//') if [ "$USAGE" -gt 90 ]; then echo "Warning: Root partition usage has reached ${USAGE}%" | \ systemd-cat -t disk-alert -p warning fi SCRIPT sudo chmod +x /usr/local/bin/disk-alert.sh # Check every hour echo "0 * * * * root /usr/local/bin/disk-alert.sh" | sudo tee /etc/cron.d/disk-alert
Last updated on