Post

Free Disk Space Linux System

Introduction

Freeing up disk space on a Linux system can be done in several ways depending on which types of files or directories are taking up the most space. Here are some common methods:

1. Identify Large Files and Directories

First, you need to identify which files and directories are using up the most space. You can do this using the du (disk usage) command:

1
sudo du -ah --max-depth=1 / | sort -hr

This command will list all files and directories in / sorted by size, from largest to smallest.

2. Clear Temporary Files

Temporary files can often take up a lot of space. You can clear them with the following commands:

System-Wide Temporary Files

1
2
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*

User-Specific Temporary Files

1
2
rm -rf ~/.cache/*
rm -rf ~/.local/share/Trash/*

3. Clean Up Package Manager Cache

If you’re using a package manager like apt (Debian-based) or yum (Red Hat-based), you can clean up the cache:

For Debian/Ubuntu:

1
2
sudo apt-get clean
sudo apt-get autoremove

For Red Hat/CentOS/Fedora:

1
2
sudo yum clean all
sudo package-cleanup --oldkernels --count=1

4. Remove Old Logs

Logs can accumulate quickly and take up a lot of space. You can rotate or remove them:

Rotate Logs

Most distributions use logrotate to manage log rotation. Ensure it is configured correctly.

Manually Remove Logs

1
sudo find /var/log -type f -name "*.log" -mtime +7 -exec rm -f {} \;

This command will delete log files that haven’t been modified for more than 7 days.

5. Remove Unused Kernel Images

If you have multiple kernel images installed, you can remove older ones:

For Debian/Ubuntu:

1
sudo apt-get autoremove --purge

For Red Hat/CentOS/Fedora:

1
sudo package-cleanup --oldkernels --count=1

6. Clean Up Docker Containers and Images (if applicable)

If you’re using Docker, you can clean up unused containers and images:

1
docker system prune -a

This command will remove all stopped containers, networks, dangling images, and build cache.

7. Uninstall Unused Software

Unused software packages can also take up space. You can uninstall them with your package manager:

For Debian/Ubuntu:

1
2
sudo apt-get purge <package_name>
sudo apt-get autoremove

For Red Hat/CentOS/Fedora:

1
sudo yum remove <package_name>

8. Check for Orphaned Files

Sometimes, files can be left over after installation or removal processes. You can use find to search for these:

1
sudo find / -type f -name "*.old" -o -name "*.bak" -o -name "*~"

9. Check Disk Quotas

If you’re on a system with disk quotas, make sure they are not causing space issues.

10. Use ncdu for Interactive Disk Usage Analysis

ncdu is an interactive tool that allows you to navigate through directory sizes and quickly identify large files:

1
2
3
4
sudo apt-get install ncdu  # For Debian/Ubuntu
sudo yum install ncdu      # For Red Hat/CentOS/Fedora

ncdu /

By following these steps, you should be able to free up significant space on your Linux system.

This post is licensed under CC BY 4.0 by the author.