How to Troubleshoot and Free Up Disk Space in Linux

"Stability is the goal of IT operations, but anomalies are the daily reality."
Photo by Bernhard / Unsplash

πŸš€

Disk space issues are a common challenge in server operations. You may receive disk space alerts only to log in and find the system warning that the disk is full. However, when trying to locate large files, the reported usage from different commands doesn’t add up.

πŸ“Œ In this guide, you’ll learn:
βœ… How to find large files and directories eating up disk space
βœ… How to handle deleted but still occupied files
βœ… How to recover reserved disk space for root users
βœ… Best practices to prevent future disk space issues


πŸ›‘ 1. Diagnosing Disk Space Issues

πŸ”Ή Step 1: Check Disk Usage

The first step in troubleshooting is to check overall disk usage:

df -h

πŸ“Œ Example Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       50G   47G  3G   94% /

Here, / (the root partition) is 94% full, leaving very little free space.


πŸ” 2. Finding Large Files and Directories

Once you confirm the disk is full, the next step is to identify which files or directories are consuming the most space.

πŸ”Ή Method 1: Use du to Find Large Directories

The du command helps analyze disk usage per directory:

du -h --max-depth=2 / 2>/dev/null | sort -hr | head -20

πŸ“Œ Breakdown:

  • --max-depth=2 limits scanning to two levels deep (adjust as needed).
  • sort -hr sorts results from largest to smallest.
  • head -20 limits the output to the top 20 results.

βœ… Faster Filtering: To find directories using gigabytes (GB) or terabytes (TB):

du -h --max-depth=2 / | grep '[GT]' | sort -hr

πŸ”Ή Method 2: Use find to Locate Large Individual Files

To find files larger than 1GB, use:

find / -type f -size +1G -exec ls -lh {} \;

πŸ“Œ Why find?

  • More efficient than du for searching specific large files.
  • Can scan the entire filesystem or specific directories.
  • -size +1G filters only files larger than 1GB.

βœ… Find and Delete Large Log Files Safely:

find /var/log -type f -name "*.log" -size +500M -exec du -h {} \;

Review files before deletion:

rm -f /var/log/large_log_file.log

πŸ› οΈ 3. Solving the "Disk Full but Files Don’t Add Up" Issue

πŸ”Ή Problem: Disk Usage Reported by df and du Doesn't Match

A common issue is when df -h reports high disk usage, but du does not show any large files. This discrepancy is usually caused by deleted files that are still in use by a running process.

πŸ”Ή Solution: Check for Deleted but Open Files Using lsof

Use lsof (List Open Files) to check for deleted files still consuming space:

lsof +L1

πŸ“Œ Example Output:

java   1234  tomcat  5r   REG  8,1  28G  /var/log/app.log (deleted)

βœ… Fix: Restart the Affected Process

systemctl restart tomcat

This releases the 28GB of occupied space.

βœ… Alternative: If restarting is not an option, you can truncate the deleted file without stopping the service:

> /proc/1234/fd/5

πŸ“Œ Replace 1234 with the process ID (PID) from lsof.


πŸ“Œ 4. Recovering Disk Space Reserved for Root

πŸ”Ή Problem: Disk Space "Missing" in df Output

Sometimes, you will notice that disk usage (Used + Available) does not equal Total Size. This is due to Linux reserving 5% of disk space for the root user to prevent critical failures when the disk is full.

πŸ”Ή Solution: Adjust Reserved Space with tune2fs

To check the current reserved space:

tune2fs -l /dev/sda1 | grep "Reserved block"

πŸ“Œ Default Reserved Blocks:

Reserved block count: 655360

To reduce reserved space from 5% to 1%:

tune2fs -m 1 /dev/sda1

πŸ“Œ Impact:

  • More disk space becomes available for normal users.
  • Ideal for non-root systems (e.g., file servers, web servers).

βœ… Check the impact:

df -h

πŸ›‘οΈ 5. Best Practices for Disk Space Management

To prevent recurring disk space issues, follow these best practices:

βœ… Set Up Log Rotation
Large log files often consume unnecessary space. Use logrotate:

sudo nano /etc/logrotate.conf

Example log rotation policy:

/var/log/*.log {
    daily
    rotate 7
    compress
    missingok
    notifempty
}

πŸ“Œ This ensures old logs are deleted or compressed automatically.

βœ… Monitor Disk Usage Regularly
Automate disk space checks:

df -h | awk '$5 ~ /[8-9][0-9]%/ {print $0}'

βœ… Alert When Disk is Almost Full
Use cron to run automatic alerts:

echo "df -h" | mail -s "Disk Space Alert!" admin@example.com

βœ… Use Separate Partitions for Logs and Data
Keeping logs in /var/log and data in /home prevents logs from filling up the entire root partition.


πŸ“Š 6. Summary

Issue Solution
Disk space is full Use df -h to check disk usage
Find large files Use `du -h --max-depth=2 /
df and du report different usage Use lsof +L1 to find deleted files still in use
Free up disk space without rebooting Restart the process holding deleted files (systemctl restart app)
Disk space is reserved for root Reduce reserved space using tune2fs -m 1 /dev/sda1
Prevent logs from filling up the disk Set up logrotate for automatic cleanup

πŸ’¬ Join the Discussion!

Have you ever run into disk space issues on a Linux server?
What methods do you use to free up space?

πŸ’¬ Share your experience in the comments below! πŸš€

πŸ‘‰ If you're troubleshooting Linux performance issues, check out: How to Fix the "Read-only File System" Error in Linux

Read more