1. Basic Commands for Checking Disk Spaceunsetunset
1.1 Check Disk Usage
# Check disk usage for all mounted points
df -h
# Check disk usage for a specific directory
df -h /home
1.2 Find Large Files and Directories
# Find files larger than 100MB in the current directory
find . -type f -size +100M -exec ls -lh {} \;
# Find files larger than 1GB in the root directory
find / -type f -size +1G -exec ls -lh {} \;
unsetunset2. Advanced Methods for Finding Large Filesunsetunset
2.1 Using the du Command
# Show the largest 10 directories in the current directory
du -sh * | sort -rh | head -10
# Show the largest 10 directories in the root directory
du -sh /* | sort -rh | head -10
# Show large files in a specific directory
du -ah /var/log | sort -rh | head -20
2.2 Comprehensive Search Script
#!/bin/bash
# Find the largest files and directories in the system
echo "=== Largest 10 Directories ==="
du -h / 2>/dev/null | sort -hr | head -10
echo -e "\n=== Largest 10 Files ==="
find / -type f -size +100M 2>/dev/null | xargs ls -lh | sort -k5 -hr | head -10
unsetunset3. Common Large File Types Analysisunsetunset
3.1 Log File Cleanup
# Check the size of the log directory
du -sh /var/log/*
# Clean old log files (keep the last 7 days)
find /var/log -name "*.log" -mtime +7 -delete
# Compress old log files
find /var/log -name "*.log" -mtime +3 -exec gzip {} \;
3.2 Cache File Cleanup
# Check the size of the cache directory
du -sh /tmp /var/tmp /var/cache/*
# Clean temporary files
rm -rf /tmp/*
rm -rf /var/tmp/*
# Clean package manager cache
yum clean all # CentOS/RHEL
apt-get clean # Ubuntu/Debian
unsetunset4. Practical Cleanup Scriptsunsetunset
4.1 Automated Cleanup Script
#!/bin/bash
# disk_cleanup.sh - Automated cleanup script for large files
LOG_FILE="/var/log/disk_cleanup.log"
# Log message function
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}
# Check disk usage
check_disk_usage() {
local usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ $usage -gt 80 ]; then
log_message "Warning: Disk usage too high: ${usage}%"
return 1
fi
return 0
}
# Cleanup log files
cleanup_logs() {
log_message "Starting log file cleanup..."
# Delete logs older than 30 days
find /var/log -name "*.log" -mtime +30 -delete
# Compress old logs
find /var/log -name "*.log" -mtime +7 -exec gzip {} \;
log_message "Log cleanup completed"
}
# Cleanup temporary files
cleanup_temp() {
log_message "Starting temporary file cleanup..."
# Clean expired temporary files
find /tmp -type f -mtime +1 -delete
find /var/tmp -type f -mtime +1 -delete
log_message "Temporary file cleanup completed"
}
# Main execution flow
main() {
log_message "=== Starting disk cleanup task ==="
if check_disk_usage; then
cleanup_logs
cleanup_temp
log_message "=== Disk cleanup task completed ==="
else
log_message "Disk usage too high, skipping cleanup operation"
fi
}
main
4.2 Scheduled Cleanup Tasks
# Add to crontab for periodic execution
# Execute cleanup every Sunday at 2 AM
0 2 * * 0 /path/to/disk_cleanup.sh
# Check disk usage every day at 3 AM
0 3 * * * df -h | grep -E "(Filesystem|/)" > /tmp/disk_usage.txt
unsetunset5. Safety Cleanup Precautionsunsetunset
5.1 Check Before Cleanup
# Check file details to avoid accidentally deleting important files
ls -la /var/log/
ls -la /tmp/
# Check file permissions and owners
ls -l /var/log/messages
5.2 Verify Cleanup Operations
# Use dry-run mode to preview files to be deleted
find /var/log -name "*.log" -mtime +7 -print
# Backup before deletion
cp /var/log/syslog /var/log/syslog.backup
rm /var/log/syslog
unsetunset6. Monitoring and Alertsunsetunset
6.1 Disk Monitoring Script
#!/bin/bash
# disk_monitor.sh - Disk monitoring script
THRESHOLD=80
EMAIL="[email protected]"
# Check disk usage
check_disks() {
df -h | grep -vE '^Filesystem|tmpfs|cdrom' | while read line; do
usage=$(echo $line | awk '{print $5}' | sed 's/%//')
partition=$(echo $line | awk '{print $1}')
mount_point=$(echo $line | awk '{print $6}')
if [ $usage -gt $THRESHOLD ]; then
echo "Warning: $mount_point disk usage $usage%"
# Send email notification
echo "Disk usage too high, please address it" | mail -s "Disk Warning" $EMAIL
fi
done
}
check_disks
6.2 Create Monitoring Service
# Create systemd service for periodic monitoring
cat > /etc/systemd/system/disk-monitor.service << EOF
[Unit]
Description=Disk Usage Monitor
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/disk_monitor.sh
User=root
[Install]
WantedBy=multi-user.target
EOF
# Enable service
systemctl enable disk-monitor.service
unsetunset7. Common Problem Solutionsunsetunset
7.1 Deleted but Still in Use Files
# Find deleted files still held by processes
lsof +L1
# Restart related services to release files
systemctl restart nginx
systemctl restart httpd
7.2 Large File Recovery
# If important files are accidentally deleted, try the following methods:
# 1. Use testdisk or photorec for recovery
# 2. Check for backups
# 3. Check filesystem logs
journalctl -f
unsetunset8. Best Practice Recommendationsunsetunset
- Regular Monitoring: Set up periodic checks for disk usage
- Backup Important Data: Ensure important data is backed up before cleanup
- Step-by-Step Cleanup: Test on a small scale before large-scale execution
- Document Operations: Keep detailed records of the cleanup process and results
- Set Thresholds: Establish a reasonable alert mechanism for disk usage
By following the above methods, you can effectively manage and maintain disk space on Linux servers, ensuring stable system operation.