Table of Contents

Reducing Disk Usage on Ubuntu

This guide provides several techniques to help free up space on your Ubuntu system. These methods can be useful if you are running low on disk space and want to optimize your system's storage.

General Clean-Up Commands

Here are some basic commands to help you get started:

sudo apt autoremove
sudo apt-get remove --purge <package-name>
sudo apt-get remove --purge $(dpkg --list | grep -P '^ii\s+linux-image-\d+' | grep -v $(uname -r) | awk '{print $2}')

Manage System Logs

System logs can accumulate over time. Here's how to manage them:

sudo journalctl --vacuum-time=2weeks
sudo find /var/log -type f -name "*.log" -exec gzip {} \;
sudo find /var/log -type f -name "*.gz" -mtime +30 -exec rm {} \;

Remove Unnecessary Files

sudo apt-get install localepurge
sudo localepurge
sudo snap remove <package-name>
sudo apt purge snapd
rm -rf ~/.cache/thumbnails/*
sudo apt-get install --reinstall deborphan
sudo deborphan --guess-data | xargs sudo dpkg --purge
sudo apt-get clean

Managing Docker Disk Usage

Docker can use a significant amount of disk space. Here’s how to manage it:

docker system prune
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Additional Tips

sudo du -ah / | sort -rh | head -n 20

Reduce Image and Video Quality: Compress images and videos using tools like `ffmpeg` or `imagemagick`.

Trim the File System: For SSDs, run the trim command to optimize performance.

sudo fstrim -v /

These methods should help you reclaim significant disk space on your Ubuntu system.

cronjob

This cron job performs routine maintenance tasks to free up disk space and optimize the system on a low-storage VPS. It includes:

This script is executed every Friday at 2:00 PM (server time) to keep the VPS lean and functioning efficiently.

0 14 * * 5 journalctl --vacuum-time=7d && docker system prune -af --volumes && apt autoremove -y && apt clean && dpkg --list | grep linux-image | grep -v $(uname -r) | awk '{print $2}' | xargs apt-get -y purge && find /var/log -type f -name "*.gz" -mtime +7 -delete && find /tmp -type f -atime +1 -delete && find /var/tmp -type f -atime +1 -delete && snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do snap remove "$snapname" --revision="$revision"; done