Why Disk Cleanup and Space Management Matters?

When your Linux server runs out of disk space, the system may become slow, applications can’t be installed or updated, and even services may fail. Regularly cleaning and managing disk space keeps the system running efficiently and avoids sudden failures. This article will teach you how to diagnose and solve disk space issues in simple terms.

I. Checking Disk Space: First Understand Where the Space Goes

1. View Overall Disk Usage

The most common command is df (Disk Free), which shows the space usage of all mounted file systems.
Example:

df -h
  • -h parameter: Converts space units to human-readable format (e.g., 1K becomes 1K, 1024K becomes 1M).

Output Explanation:
| Column Name | Meaning |
|--------------|----------------------------------------------------------------------|
| Filesystem | Disk device name (e.g., /dev/sda1) |
| Size | Total disk capacity |
| Used | Used space |
| Avail | Available space |
| Use% | Usage rate (alert if over 80%) |
| Mounted on | Directory where the disk is mounted (e.g., / for root, /home for user home) |

Note: df -h shows all file systems, including temporary ones (e.g., /tmp), but temporary files are usually safe to clean.

2. Check Size of a Single Directory/File

If df shows overall disk fullness, use du (Disk Usage) to locate which directory is taking up space.
Example 1: Total size of a directory

du -sh /var/log
  • -s parameter: Only show total size of the directory (no recursion into subdirectories); -h as before for human-readable units.
  • To see subdirectories under /var/log, remove -s: du -h /var/log.

Example 2: Recursively find large files
To find all files over 100MB in the system:

find / -type f -size +100M 2>/dev/null
  • -type f: Only find regular files (exclude directories, devices, etc.);
  • -size +100M: File size exceeds 100MB;
  • 2>/dev/null: Suppress error messages (e.g., inaccessible directories).

II. Disk Cleanup: Remove “Unnecessary” Space

1. Clean Log Files (Most Common “Space Hogs”)

Log files (e.g., under /var/log) are major space consumers, especially system logs and application logs (e.g., Nginx, MySQL logs).
Steps:
- Step 1: Check log directory size

  du -sh /var/log/*

Identify large log files (e.g., nginx/access.log, syslog).

  • Step 2: Clean old logs
    Logs can be deleted directly (confirm they are backed up or unnecessary):
  # Check log content first (e.g., Nginx access log)
  tail /var/log/nginx/access.log

  # If confirmed useless, truncate or delete (recommended: truncate avoids service interruption)
  > /var/log/nginx/access.log  # Truncate the log file

  # Or delete old numbered logs (e.g., .log.1, .log.2)
  rm -f /var/log/nginx/access.log.1

Tip: System logs are usually rotated automatically by logrotate (generates old logs and truncates the current log).

2. Clean Cache and Temporary Files

Linux caches data to memory for performance, but if memory is low, cache may occupy significant disk space.
Clear system cache:

# 1. Sync data to disk (prevent data loss)
sync

# 2. Free cache (requires root privileges)
sudo sysctl -w vm.drop_caches=3
  • vm.drop_caches=3: Release all caches (page cache, inode cache, etc.). The system will automatically rebuild caches as needed.

Clean temporary files:
Temporary files (e.g., /tmp, /var/tmp) can be safely cleaned:

# Clean all files in /tmp (temporary files may be in use, confirm first)
sudo rm -rf /tmp/*

# Clean apt cache (Debian/Ubuntu; similar for yum/dnf)
sudo apt clean  # Clear all downloaded package caches
sudo apt autoclean  # Only clear outdated package caches

3. Clean User Data and Old Files

User directories (e.g., /home) or shared directories may store redundant files:
Steps:
- Check large files in home directory:

  du -sh ~/.*  # ~ represents current user's home directory
  cd ~/Downloads  # Check downloads folder
  du -sh *  # View subdirectory sizes
  • Delete unnecessary files:
    Warning: Always confirm file usage before deletion!
  # Delete a specific file
  rm ~/Downloads/large_file.zip

  # Delete multiple files (e.g., all .tmp files)
  rm ~/tmp/*.tmp

Safe operation: If unsure, list files first and confirm:

ls ~/old_data  # List directory contents
rm -i ~/old_data/unused_file  # -i prompts for confirmation before deletion

III. Expansion Ideas When Space Is Insufficient

If cleanup isn’t enough, consider expanding the disk:

If the server has an unmounted disk:

# Check new disk device name (e.g., /dev/sdb)
lsblk

# Format the disk (only once, confirm device name!)
sudo mkfs.ext4 /dev/sdb

# Create mount point directory
sudo mkdir /mnt/new_disk

# Mount the disk
sudo mount /dev/sdb /mnt/new_disk

# Permanently mount (persists across reboots)
sudo vim /etc/fstab  # Add:
/dev/sdb   /mnt/new_disk   ext4   defaults   0   0

2. Resize Partitions (Caution Required)

To expand an existing partition (e.g., root partition /), use tools like parted or gparted. ⚠️ High risk: Always backup data first!
- Steps: Unmount partition → Resize → Remount.
- Recommendation: Contact your server admin if unfamiliar with partition operations.

IV. Daily Maintenance Recommendations

  1. Regular disk space checks:
    Use df -h daily/weekly; act when usage exceeds 80%.
  2. Set up log rotation:
    Configure logrotate to auto-clean old logs (e.g., Nginx, syslog).
  3. Avoid storing data in root directory:
    Place important data in /home, /data, etc., to reduce root partition pressure.
  4. Do not randomly delete system files:
    Be cautious with files in /etc, /bin, /usr to avoid system damage.

Summary

The core of disk cleanup and space management is “Locate large files → Clean unnecessary data → Regular maintenance”. Use df and du for quick diagnosis, prioritize cleaning logs, caches, and temporary files, then consider expansion. Develop the habit of regular checks to avoid disk space issues. Remember: backup data before operations, confirm file usage before deletion, and use rm with caution!

Xiaoye