Effective disk space management is crucial for maintaining system performance and stability. As you use your Linux system, files and packages accumulate, potentially filling up your storage and causing issues such as slow performance and system crashes.
In this article, we will learn various methods and commands to reclaim valuable disk space on Linux system. You will learn how to check disk usage, remove unnecessary packages, clear caches, delete old backups and log files, compress large files, and save considerable disk space on Linux.
Table of Contents
Check disk usage in Linux
du (disk usage) command is a powerful tool that analyzes disk usage for files and directories. It helps identify which files or directories consume the most space on your system.
To analyze disk usage for a specific directory, use the following command –
du -h --max-depth=1 /path/to/directory
-h: Human-readable output (displays sizes in a human-readable format).
--max-depth=1: Limits the depth of the analysis to the top-level directory.
/path/to/directory with the directory you want to analyze.
du command will display a list of directories and their sizes in human-readable format. Review the output to identify large directories you may want to investigate further.
Free up disk space on Linux
1. Removing Unused Packages
Ubuntu and Linux Mint use
apt as the default package manager. We can manage installed packages efficiently to free up space.
To list all installed packages, use the following command –
To remove an unused package, use the following command –
sudo apt remove package-name
package-name with the name of the package you want to remove.
Remove Orphaned Dependencies
To remove orphaned dependencies (packages that are no longer needed), use the following command –
sudo apt autoremove
2. Clearing Cache
Caches store temporary data to improve system and application performance. However, they can grow over time and consume disk space.
To clear the package cache, use the following command –
sudo apt clean
Browser caches can be removed through the browser’s settings. For Firefox, open the browser and navigate to
Settings > Privacy & Security > Clear Data. For Chromium, visit
Settings > Privacy and Security > Clear browsing data.
To clear the thumbnail cache used by the file manager, use the following command –
rm -r ~/.cache/thumbnails
To clear cache in one click, we can use apps like Bleachbit. Bleachbit is a CCleaner counterpart on Linux. It’s free and available for free from all distro repositories.
sudo apt install bleachbit
3. Removing Old Backups
Regular backups are important, but outdated backups can accumulate and take up space.
To list backup files in a directory, use a command like
find. For example:
find /path/to/backups -type f -mtime +30
This command lists files older than 30 days in the specified directory.
To delete old backups, you can use the
rm command with the
-r option (be cautious with this command) –
rm -r /path/to/backups
/path/to/backups with the actual path to your backup directory.
4. Removing Old Log Files
Log files are essential for system monitoring and debugging, but older ones can take up space unnecessarily.
To list log files, navigate to the
/var/log directory –
ls -lh /var/log
To delete old log files, you can use the
rm command, but exercise caution and consider archiving necessary logs –
sudo rm /var/log/*.log.1
This example removes log files with a
.log.1 extension, which are typically rotated logs.
5. Compressing Large Files
File compression reduces the size of files and directories, saving disk space.
To compress a file or directory, use the
gzip commands together. For example –
tar -czvf compressed-file.tar.gz /path/to/large-file-or-directory
This creates a compressed archive.
To extract compressed files, use the following command –
tar -xzvf compressed-file.tar.gz
This article covers various methods and commands to help you regain valuable disk space on Linux system. Managing your disk space efficiently is not just about preventing out-of-space errors; it’s about maintaining optimal system performance and ensuring a smooth computing experience.
Remember that while these commands are powerful tools, exercising caution is crucial. Always double-check the files and directories you intend to delete or compress, and back up critical data before making significant changes to your system.
By following the guidance in this article, you’re well on your way to becoming a master of disk space management in the world of Linux. Keep your system tidy, and enjoy the benefits of a well-maintained, efficient, and spacious environment for all your computing needs.