可以的。Ubuntu 可以自动进行 cleanup(清理),常见做法有几种,从“系统自带自动清理”到“定时任务自动清理”都可以。下面按由简到实用的方式给你说明。
Ubuntu 默认会保留已下载的 .deb 缓存,但可以自动删除不用的版本:
sudo apt install unattended-upgrades
然后确保以下配置存在(/etc/apt/apt.conf.d/50unattended-upgrades):
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";
✅ 效果:
apt autoremove你可以创建一个 systemd timer,比如每周清理一次。
sudo nano /etc/systemd/system/ubuntu-cleanup.service
内容:
[Unit]
Description=Ubuntu Cleanup
[Service]
Type=oneshot
ExecStart=/usr/bin/apt-get autoremove -y
ExecStart=/usr/bin/apt-get autoclean -y
ExecStart=/bin/journalctl --vacuum-time=7d
sudo nano /etc/systemd/system/ubuntu-cleanup.timer
内容:
[Unit]
Description=Run Ubuntu Cleanup Weekly
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
启用:
sudo systemctl enable --now ubuntu-cleanup.timer
✅ 每周自动:
如果你更习惯 cron:
sudo crontab -e
添加:
0 3 * * 0 apt-get autoremove -y && apt-get autoclean -y
意思是:每周日 3 点自动清理
你可以根据需求加入:
# 清理缩略图
rm -rf ~/.cache/thumbnails/*
# 清理旧内核(谨慎)
apt-get autoremove --purge
# 清理 snap 旧版本
snap list --all | awk '/disabled/{print $1, $3}' | while read snap rev; do snap remove "$snap" --revision="$rev"; done
⚠️ 避免写:
rm -rf /var/log/*
容易造成:
✅ 新手推荐:
unattended-upgradesRemove-Ununused-Dependencies✅ 进阶推荐:
如果你愿意,我可以: