在 Ubuntu 上做“自动化清理”通常分三层:临时/缓存清理、日志清理、包与系统清理,再通过 cron / systemd timer 实现自动化。下面给你一套实用方案。
sudo apt autoremove -y
sudo apt autoclean -y
sudo apt clean -y
rm -rf ~/.cache/*
sudo journalctl --vacuum-time=7d
sudo find /var/log -type f -name "*.log" -mtime +7 -delete
sudo apt purge $(dpkg -l | grep 'linux-image' | awk '{print $2}' | grep -v `uname -r`)
sudo crontab -e
添加:
0 3 * * 0 apt autoremove -y && apt autoclean -y && journalctl --vacuum-time=7d
✅ 适合:个人服务器、轻量 VPS
sudo nano /usr/local/bin/ubuntu-clean.sh
内容:
#!/bin/bash
apt autoremove -y
apt autoclean -y
journalctl --vacuum-time=7d
rm -rf /tmp/*
sudo chmod +x /usr/local/bin/ubuntu-clean.sh
sudo nano /etc/systemd/system/ubuntu-clean.service
[Unit]
Description=Ubuntu Cleanup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/ubuntu-clean.sh
sudo nano /etc/systemd/system/ubuntu-clean.timer
[Unit]
Description=Weekly Ubuntu Cleanup
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl enable --now ubuntu-clean.timer
查看:
systemctl list-timers
✅ 适合:生产环境、服务器
bleachbit(GUI + CLI)sudo apt install bleachbit
sudo bleachbit --clean apt.cache
unattended-upgrades(自动安全更新)sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
| 项目 | 频率 |
|---|---|
| apt autoremove | 每周 |
| journalctl | 7 天 |
| /tmp | 重启或每周 |
| 日志 | 7–30 天 |
| 内核 | 手动或脚本判断 |
如果你愿意,我可以:
你主要用在什么环境?