ncdu or df -h. Resolving this involves cleaning up these specific areas to reclaim space and ensure system stability without removing essential operational files.
Identifying the Culprits
Before attempting any cleanup, it is crucial to understand where the storage is being consumed. The first step is to check the overall disk utilization to see if the root partition or a specific mount point is near capacity. Run the following command in your terminal:df -h
This output shows the total size, used space, available space, and percentage of use for each mounted filesystem. If you see a partition with high usage, such as / (root) or /home, you need to drill down further. A powerful tool for visualizing directory sizes is ncdu (NCurses Disk Usage). Install it via:
sudo apt install ncdu
Then run sudo ncdu / to navigate through the file system interactively. This allows you to press 'd' to delete files directly within the tool, which is safer than guessing. Alternatively, for a quick summary of the top ten largest directories, use:
du -sh /* | sort -rh | head -n 10
Common offenders include the /var/log directory for old journal logs, /var/cache/apt for downloaded package binaries, and /usr/share/doc for documentation that may not be frequently accessed. Understanding these locations helps target your cleanup efforts effectively.
Cleaning Package Manager Cache
Ubuntu’s APT package manager stores downloaded.deb files in /var/cache/apt/archives/. Over time, this cache can grow significantly, especially if you have installed many applications or performed frequent updates. To clear this cache safely, use the following command:
sudo apt clean
This removes all cached package files from the local repository. It does not uninstall any software; it only deletes the installation files so they must be re-downloaded if you reinstall a package later. If you want to remove only obsolete packages that are no longer needed by the system, run:
sudo apt autoremove
This command checks for dependencies that were automatically installed to support other programs but are no longer required. Removing them frees up space and keeps your package database tidy. For a more aggressive cleanup that also removes configuration files of uninstalled packages, you can combine these steps, but always review the list of packages to be removed before confirming.
Managing System Logs
The/var/log directory often contains large log files from the systemd journal and various services. Journal logs can accumulate rapidly, especially on systems with frequent restarts or errors. To view the current size of the journal, use:
journalctl --disk-usage
To limit future growth, edit the configuration file at /etc/systemd/journald.conf. Find the line #MaxUse= and change it to something like MaxUse=50M to cap the total size. Similarly, set MaxFileSec=1week to rotate logs weekly. After making changes, restart the journal service:
sudo systemctl restart systemd-journald
You can also manually vacuum the journal to immediately free space:
sudo journalctl --vacuum-time=2d
This keeps only logs from the last two days. Be cautious when deleting individual log files manually, as some services may write to them continuously. Using the journalctl commands ensures proper rotation and handling.
Handling Old Kernels
Each kernel update installs new headers and modules while keeping older versions for fallback. Over multiple updates, this can consume several gigabytes. List installed kernels with:dpkg --list | grep linux-image
Keep the currently running kernel (check with uname -r) and the previous one for safety. Remove older versions using:
sudo apt purge linux-image- linux-headers-
Replace <version> with the specific version numbers you wish to remove. This process is safe as long as you retain the most recent stable kernel.
Checking for Large User Files
Sometimes the issue lies within user home directories. Videos, downloads, and virtual machine images can take up substantial space. Use thefind command to locate files larger than 100MB:
find ~ -type f -size +100M -exec ls -lh {} \; 2>/dev/null | awk '{ print $9 ": " $5 }'
Review these files to determine if they are necessary backups or temporary downloads. Additionally, check for core dumps in the root directory or home folders, which are created when applications crash unexpectedly. These can be deleted safely if you do not need them for debugging.
For users managing containers, similar issues arise with Docker volumes and images. If you suspect container-related bloat, refer to our guide on Troubleshooting Docker Container Unable to Write to Removable Drive to understand how storage management impacts container performance and space allocation. Properly pruning unused images and containers is essential for maintaining healthy disk usage in containerized environments.
Preventive Measures
To prevent future storage crises, automate regular cleanups. Add theapt clean and autoremove commands to a monthly cron job. Monitor disk usage with tools like ncdu periodically. Consider moving large data directories like /var/log or user media to separate partitions if storage becomes a recurring bottleneck. Regular maintenance ensures your Ubuntu 20.04.6 LTS system remains responsive and stable.
Frequently Asked Questions
Is it safe to delete files in /var/log?
No, you should never manually delete files in /var/log while services are running, as this can cause errors. Instead, use journalctl commands to vacuum logs or configure journald.conf to limit their size automatically.
How do I find the largest files in my home directory?
You can use the find command with the -size flag to locate files exceeding a specific size threshold. Combining this with ls -lh provides a human-readable list of these large files for easy review.
What happens if I purge an old kernel?
Purging an old kernel removes its modules and headers, freeing up significant space. However, you cannot boot into that kernel version if your current one fails, so always keep at least one previous kernel as a fallback.
Can I move /var/log to another drive?
Yes, you can bind-mount a larger partition to /var/log. This requires creating the directory on the new drive and adding an entry to /etc/fstab to mount it at boot, ensuring logs are stored elsewhere.
Why does my disk usage show high even after cleaning?
Some processes hold open file handles to deleted files, preventing space from being reclaimed until the process restarts. Rebooting the system releases these handles and updates the actual available disk space.