在Linux系统中,磁盘清理工具的选择需根据使用场景和技术水平而定。以下是主流工具的深度对比及选型建议:
ncdu (NCurses Disk Usage)
bash
sudo apt install ncdu
ncdu / --exclude /mnt # 排除挂载点
du + find组合
bash
# 查找大于100MB的文件
find /path -type f -size +100M -exec du -h {} + | sort -rh
# 清理7天前日志
find /var/log -type f -mtime +7 -delete
BleachBit
bash
bleachbit --clean firefox.* system.*
Baobab (Disk Usage Analyzer)
FileLight (KDE)
日志清理
journalctl --vacuum-size=200M # 限制systemd日志大小
logrotate -f /etc/logrotate.conf # 强制轮转日志
Docker/容器清理
docker system prune -af # 清理所有未用对象
podman system prune
包缓存清理
apt clean # Debian
dnf clean all # RHEL
pacman -Scc # Arch
Ansible Playbook自动化清理
- name: Clean temp files
hosts: servers
tasks:
- name: Remove /tmp files older than 30d
ansible.builtin.file:
path: /tmp
state: absent
age: "30d"
Prometheus+Alertmanager
node_filesystem_avail_bytes / node_filesystem_size_bytes
ncdu
+ find
+ 日志轮转BleachBit
+ Stacer
(系统优化套件)rm -rf ~/.cache/*
shred
bash
shred -uvz file.txt # 覆盖后删除
-exec rm -i
或--dry-run
参数bash
chattr +i /critical_dir # 禁止修改
终极建议:将磁盘清理纳入日常运维流程,结合cron
定期执行(示例):
# 每周日凌晨清理
0 0 * * 0 /usr/bin/find /tmp -type f -mtime +7 -delete
根据实际需求组合使用上述工具,可高效保持系统整洁。对于生产环境,务必先在测试环境验证清理命令!