在 Linux 上做“自动化清理”通常分三步:明确清理对象 → 选择清理方式 → 定时自动化。下面按实战角度给你一套可落地方案。
/var/log/*/var/cache/*/tmp/var/tmp~/.cache# 清理 systemd 日志(保留最近 7 天)
journalctl --vacuum-time=7d
# 清理 apt 缓存
apt-get clean
apt-get autoclean
# 清理 yum/dnf
dnf clean all
rm -rf /tmp/*
rm -rf /var/tmp/*
apt-get autoremove --purge
docker system prune -af
/usr/local/bin/auto-clean.sh
#!/bin/bash
# 自动清理脚本
# 日志
journalctl --vacuum-time=7d
# 包缓存
apt-get clean
apt-get autoclean
# 临时文件
find /tmp -type f -mtime +7 -delete
find /var/tmp -type f -mtime +7 -delete
# Docker
docker system prune -af
echo "$(date) clean done" >> /var/log/auto-clean.log
赋予权限:
chmod +x /usr/local/bin/auto-clean.sh
crontab -e
每周日凌晨 3 点执行:
0 3 * * 0 /usr/local/bin/auto-clean.sh
/etc/systemd/system/auto-clean.service
[Unit]
Description=Auto Clean
[Service]
Type=oneshot
ExecStart=/usr/local/bin/auto-clean.sh
/etc/systemd/system/auto-clean.timer
[Unit]
Description=Weekly Auto Clean
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
启用:
systemctl enable --now auto-clean.timer
rm -rf / 或模糊路径find -mtime 控制时间,避免误删bleachbit(图形/命令行清理)ncdu 查大文件如果你能告诉我:
我可以直接给你一份可直接用的定制脚本。