How to Troubleshoot and Free Up Disk Space in Linux
π
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=2limits scanning to two levels deep (adjust as needed).sort -hrsorts results from largest to smallest.head -20limits 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
dufor searching specific large files. - Can scan the entire filesystem or specific directories.
-size +1Gfilters 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