When your Linux server suddenly pops up a “disk space full” warning, do you feel a bit overwhelmed? Don’t panic! Insufficient disk space is a common issue for beginners, but with a few simple cleanup techniques, you can get your server back to running smoothly. This article will guide you through fixing disk space problems using basic commands and steps, keeping it simple and easy to follow.
I. First, Check How Much Disk Space is Used¶
To know “where the space is being occupied,” you first need to check the overall disk usage. The most common command in Linux is df -h, which displays disk partition usage in a human-readable format (e.g., GB, MB).
df -h
After execution, you’ll see output like this:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 15G 5.0G 75% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
Here, the Used column shows occupied space, and Avail shows available space. Focus on system directories like the root directory / or /var—these are often the biggest space hogs.
II. Find the “Space Thieves” — Locate Large Files/Folders¶
Once you understand the overall situation, you need to identify which files or directories are taking up space. The most useful tools are du -sh (shows directory sizes) and find (searches for files).
1. Quickly Locate Large Directories¶
For example, to check subdirectories under /var:
du -sh /var/*
Output might look like:
4.0K /var/backups
1.2G /var/cache
500M /var/lib
This helps identify suspects like /var/cache or /var/lib.
2. Find Large Files (e.g., over 100MB)¶
Use the find command to search for files larger than 100MB (note: searching the entire / may take time; start with /home or /tmp for user directories first):
find / -type f -size +100M 2>/dev/null
-type f: Only search for files (not directories);-size +100M: Files larger than 100MB;2>/dev/null: Ignore permission errors to avoid distraction.
III. Targeted Cleanup: Focus on These “Space Killers”¶
1. Clean Log Files (/var/log Directory)¶
Log files are a hidden space hog, especially in /var/log (system logs, application logs). Do NOT delete the entire directory!
- Method 1: Empty Specific Logs (Proceed with Caution)
Some logs may no longer be active (e.g., old system logs). You can clear them with:
echo "" > /var/log/syslog
Note: If the log file is still being written (e.g., using tail -f /var/log/syslog shows live updates), direct deletion may cause data loss!
- Method 2: Use logrotate for Automatic Log Management
Linux’s built-inlogrotatetool automatically rotates logs (e.g., compresses old logs) to prevent infinite growth. If configured, force a rotation with:
sudo logrotate -f /etc/logrotate.conf
- Method 3: Delete Old Log Compression Files
Some logs are compressed into.gzor.bz2files (e.g.,messages-202301.gz). Once confirmed as unnecessary, delete them:
sudo rm /var/log/*.gz
2. Clean Cache and Temporary Files¶
System cache and temporary files (/tmp, /var/tmp) also occupy space. Clean them with these steps:
- Free System Cache
Linux caches frequently accessed files in memory. If memory is sufficient but disk space is low, clear the cache (no data loss risk—this only releases system cache):
sudo sync # Sync data to disk to avoid loss
echo 3 > /proc/sys/vm/drop_caches # Release all caches
- Clean Temporary Files
/tmpis a temporary file hotspot; most files disappear after reboot, but some may linger. Direct deletion:
sudo rm -rf /tmp/* /var/tmp/*
Note: If a program is using files in /tmp (e.g., temporary files from a running app), deletion may cause errors. Check if files are in use with lsof /tmp/xxx before deleting.
3. Uninstall Unneeded Packages¶
If you’ve installed unnecessary software (e.g., old tools, test packages), uninstall them via the package manager to free space.
- For CentOS/RHEL (using
yum):
sudo yum remove 软件包名称 # Uninstall a single package
sudo yum clean all # Clear cached installation packages (.rpm files)
- For Ubuntu/Debian (using
apt):
sudo apt remove 软件包名称 # Uninstall a single package
sudo apt autoremove # Remove unused dependencies
sudo apt clean # Clear cached installation packages (.deb files)
4. Clean Large User Directory Files¶
For personal user servers, /home may contain large files (e.g., downloaded movies, installation packages). Have users check their directories:
cd ~ # Enter the current user’s home directory
du -sh * # View sizes of all files/folders in the directory
If you find large unnecessary files (e.g., multi-GB zip files), delete them directly:
rm -rf 大文件名称 # Use with caution!
IV. Important Notes: Avoid These Common Pitfalls¶
- Do not delete critical system files: Avoid deleting files in
/etc,/bin, or/usr—this can break the system! - Always verify before deleting: Check file contents first (e.g.,
cat /var/log/syslogto confirm if a log is still active). - Be cautious with non-root operations: Deleting system directories (e.g.,
/var) or modifying system files (e.g.,/etc) requiressudopermissions. - Regular maintenance: Develop a habit of checking disk space weekly to catch issues early and prevent larger problems.
V. Summary: Cleanup Priority Steps¶
- Use
df -hto check overall disk usage. - Use
du -shto identify large directories. - Use
findto locate large files. - Prioritize cleaning logs, cache, and temporary files.
- Uninstall unnecessary packages and delete large user files.
By following this process, even beginners can easily free up disk space. Remember, Linux is stable, and as long as you don’t blindly delete critical files, the cleanup process is safe! If unsure about a file, back it up or consult documentation—safety first!