As you use your Linux system, files and logs accumulate over time. These files can include temporary data, cached files, and outdated logs. Left unmanaged, they take up valuable disk space and can slow down your system. Checking and cleaning your system is essential to prevent it from filling up.
Reclaiming disk space in Linux can be done using standard system commands and tools. Some methods work across distributions, while others are specific to distributions like Ubuntu, Debian, or Fedora. It's important to know which method works best for your system and follow appropriate steps to remove unnecessary files.
Clearing disk space involves identifying large files, cleaning up system logs, and removing unused applications. Understanding how to check disk usage and remove unnecessary files will ensure your system performs efficiently. Regular cleanup is key to maintaining free disk space on Linux.
Steps to clear disk space on Linux:
- Check the disk usage for all mounted drives and file systems.
$ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 391M 1.9M 389M 1% /run /dev/sda3 20G 7.2G 11G 40% /
Use the df command to show the disk space usage of each mounted file system. Pay attention to the percentage in the Use% column.
- Identify directories that consume significant space on each file system.
$ du -h --max-depth=1 / 2.0G /var 1.2G /usr 500M /home
The du command helps identify which directories consume the most space. Focus on directories like var and home.
- Locate large files and temporary files that are safe to delete.
$ sudo find / -type f -size +500M /data/largefile.img /var/log/biglog.log
Use the find command to locate large files, such as those above 500MB, that may be taking up valuable disk space.
- Delete unnecessary files from directories such as Downloads and Trash.
$ rm -rf ~/Downloads/* $ rm -rf ~/.local/share/Trash/files/*
Empty the Downloads and Trash directories regularly, as they often contain unneeded files.
- Remove unused packages and dependencies that are no longer required.
$ sudo apt autoremove $ sudo yum autoremove
Run the autoremove command to remove unused dependencies and outdated packages. This works for both Ubuntu (apt) and Fedora (yum) based systems.
- Clear cached package installer files to free up space.
$ sudo apt clean $ sudo yum clean all
This clears the package cache from the system, freeing up additional space.
- Purge old system logs and journal entries.
$ sudo journalctl --vacuum-time=7d
System logs can accumulate quickly. Use the journalctl command to remove logs older than 7 days.
- Remove user-specific application data if it's not needed.
$ du -sh ~/.cache 500M /home/user/.cache $ rm -rf ~/.cache/*
User-specific caches, stored in the .cache directory, can grow large over time. Deleting them frees up space without affecting system operation.
- Clean up directories left behind by deleted users.
$ sudo rm -rf /home/olduser/
Check for home directories of users no longer present on the system. These can be safely deleted to free up space.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.