Running out of space on your Ubuntu root directory (/
) can lead to system performance issues and even prevent updates or installations. If your /
directory is nearing its capacity, it’s time for some cleanup. Follow these steps to efficiently free up space on your Ubuntu system.
The first step is to find what’s taking up the most space.
sudo du -xh / | grep -P "^[0-9.]+G" | sort -hr | head -n 20
This command lists the top 20 largest files and directories. It gives you an idea of where to start.
Package managers often leave behind cached files. Clean them up with:
sudo apt-get clean sudo apt-get autoremove
Old kernels can occupy significant space. List installed kernels:
dpkg --list 'linux-image*'
Remove the old ones, ensuring not to remove the current kernel:
sudo apt-get remove --purge linux-image-<old-kernel-version>
System logs can grow large over time. Limit them to the last few days:
sudo journalctl --vacuum-time=3d
Identify and remove orphaned packages using deborphan
:
sudo apt-get install deborphan sudo deborphan | xargs sudo apt-get -y remove --purge
Search for large files manually:
sudo find / -type f -size +100M -exec ls -lh {} \\;
Review and delete unnecessary files.
Clear out temporary files:
sudo rm -rf /tmp/* sudo rm -rf /var/tmp/*
ncdu
Use ncdu
to interactively check disk usage:
sudo apt-get install ncdu sudo ncdu /
This tool helps you navigate and identify large directories.
Check specific directories known for accumulating files:
sudo ls -lh /var/log sudo ls -lh /var/cache
Manually remove unnecessary files.
Snap packages can also take up space. List and clean up old revisions:
sudo snap list --all sudo snap remove <old-snap-revision>