SoFunction
Updated on 2025-05-04

Solution to deal with insufficient disk space under Linux

Problem description

Insufficient disk space is one of the most common problems during Linux system operation and maintenance. When the system prompts "No space left on device" or passdf -hWhen the command finds that the disk usage rate is close to 100%, the system performance will drop significantly, and in severe cases it may even cause service crashes. This article will introduce in detail how to diagnose and resolve disk space issues under Linux.

Step 1: Confirm the disk usage

First, you need to fully understand the disk usage status of the system:

# Check the usage of each partitiondf -h

# Check the file/directory size in the current directorydu -sh *

df -hThe command displays the usage of each mount point, anddu -sh *You can view the space occupied by each project in the current directory.

Step 2: Position large files and directories

1. Find large files

# Find files larger than 100MBfind / -type f -size +100M -exec ls -lh {} ;

# Or use more intuitive commandsfind / -type f -size +100M -exec du -h {} ; | sort -n

2. Find the large directory

# Check the size of each subdirectory in the /var directorydu -sh /var/* | sort -h

# Or use ncdu tools (need to be installed)ncdu /

Step 3: Common space occupation sources and cleaning methods

1. Log files

/var/log directory is often a "severely affected area" of space:

# Check the log file sizels -lh /var/log/

# Clean old logs (operate with caution)sudo journalctl --vacuum-size=100M  # Keep the last 100MB logsudo rm /var/log/*.gz /var/log/*.old

2. Temporary documents

# Clean up /tmp directorysudo rm -rf /tmp/*

# Clean up temporary system filessudo rm -rf /var/tmp/*

3. Package Cache

For Debian-based systems:

sudo apt-get clean
sudo apt-get autoclean

For RHEL-based systems:

sudo yum clean all
sudo dnf clean all

4. Docker containers and images

# Check Docker disk usagedocker system df

# Clean up useless resourcesdocker system prune -a

5. Old kernel version

# View installed kernelsdpkg --list | grep linux-image

# Remove the old kernel (retain the last 2-3 versions)sudo apt-get purge -xx-generic

Step 4: Advanced Cleaning Tips

1. Find and delete duplicate files

# Use the fdupes tool (need to be installed)fdupes -r /home | grep -v '^$'

2. Find and delete empty files

find / -type f -empty -delete

3. Find and delete files of a specific type

# For example, delete all .log files that have been over 1 yearfind /var/log -name "*.log" -type f -mtime +365 -delete

Step 5: Preventive measures

  • Set log rotation: Configure /etc/ files to ensure that log files rotate and compress regularly
  • Monitor disk space: Set up cron tasks or use monitoring tools such as Prometheus to check disk space regularly
  • Reasonable partitioning: Partition the easy-to-growth directories such as /var, /home, and separate partitions
  • Using LVM: Easy to expand disk space in the future

Summarize

To deal with insufficient Linux disk space, we need to systematically troubleshoot and clean up. Through the methods described in this article, you can quickly locate the space occupancy source and take corresponding measures. Remember to be extra careful when cleaning system files to avoid deleting important system files that lead to system instability.

This is the end of this article about the solution to the problem of insufficient disk space under Linux. For more related content on insufficient disk space, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!