Troubleshooting Low Disk Space on Linux

Insufficient disk space is one of the most common and critical issues in Linux system administration. When key partitions such as /, /home, or /var run out of space, the system becomes unable to write logs, start services, or even crash. This article aims to provide a systematic troubleshooting and cleanup methodology to help administrators quickly identify abnormally large files and free up space.

Confirm Disk Usage Status

Before diving into analysis, it is essential to accurately grasp the current disk usage status. Linux offers various tools to monitor and report disk space, among which df is the most fundamental and widely used command.

Running df -h lists the remaining space on all mounted points in a human-readable format (e.g., GB, MB). For more detailed analysis, it is recommended to combine the -T parameter to view file system types or use the du command to sort directories by size.

# View total usage of all partitions
df -h

# Recursively view file usage under a specified directory (sorted by size in descending order)
du -ah /var/log | sort -hr | head -n 10

Through these commands, you can quickly identify which partition is running low and which subdirectories within that partition are consuming significant space. For example, /var/log or /tmp are often hotspots for accumulated logs and temporary files.

Analyze Abnormal Files and Directories

Once a suspicious directory has been located, further investigation is needed to determine exactly which file or process is causing the waste of space. Sometimes, seemingly normal directories consume vast amounts of space because they contain millions of small files; in such scenarios, the traditional ls command is extremely inefficient and difficult to read.

You can use find combined with du for a deep scan, or utilize graphical interactive tools like ncdu (which needs to be installed first) to intuitively view disk usage distribution. If you need to locate specific large files, the following script logic is very practical:

  1. Find the top N largest files within a specified directory:
    find /path/to/directory -type f -exec du -h {} + | sort -hr | head -n 5
    
  2. Locate large entries within log files:
    Certain applications (such as PostgreSQL, MySQL, or Web servers) may generate massive single-line log records. You can quickly filter them using find combined with the -size parameter:
    find /var/log -name "*.log" -type f -size +100M -exec ls -lh {} \;
    

Safe Cleanup Strategies and Operations

After confirming the source of space consumption, take corresponding cleanup measures based on file types. Blindly deleting files carries a risk of data loss and must follow the principle of "backup first, delete later."

  • Clean temporary files:
    The /tmp and /var/tmp directories are typically configured for automatic cleanup, but they may accumulate old data in certain cases. You can use the find command to locate files not modified for a certain period and delete them:

    find /tmp -type f -mtime +7 -delete
    # Note: Ensure important temporary data is backed up before executing in production environments
    
  • Handle log files:
    For log files, it is recommended to use the logrotate tool for rotation configuration, which automatically compresses old logs and deletes expired files. If cleaning manually, use the -v parameter with rm to confirm the deletion operation:

    rm -v /var/log/old_application.log
    
  • Clean database files:
    If space is occupied by databases, do not directly delete .ibd or .sql files. First, use database management tools to execute VACUUM (PostgreSQL) or OPTIMIZE TABLE (MySQL/MariaDB). Once data organization is complete, proceed with archiving or deletion operations.

Prevention Mechanisms and Long-term Maintenance

Establishing long-term mechanisms to prevent recurrence is just as important after resolving the current issue.

  1. Configure Disk Monitoring:
    Utilize systemd's timedatectl or third-party tools like smartmontools combined with scripts to automatically send alert emails when disk usage exceeds a threshold (e.g., 80%).

  2. Optimize Log Policies:
    Configure reasonable rotation policies in /etc/logrotate.conf to ensure daily archiving and weekly compression, preventing logs from growing indefinitely.

  3. Set Up Regular Cleanup Schedules:
    Use cron to set up scheduled tasks that automatically scan and clean temporary files and large files older than a certain number of days on a weekly basis, maintaining a tidy system environment.

By following the steps above, you can efficiently resolve issues related to insufficient disk space and build a robust disk management process to ensure stable operation of the Linux system.