以下是Debian系统清理临时文件的常用技巧,涵盖工具使用和注意事项:
tmpreaper
(Debian默认工具,类似tmpwatch
)
sudo apt-get install tmpreaper
sudo tmpreaper 7d /tmp
(删除7天内未访问的文件)/etc/cron.daily/tmpreaper
或添加crontab -e
规则,例如每天凌晨清理:0 0 * * * /usr/sbin/tmpreaper 1d /tmp
systemd-tmpfiles
(系统级管理)
/etc/tmpfiles.d/cleanup.conf
,添加w /tmp 7d
(7天后删除/tmp文件)sudo systemd-tmpfiles --clean
apt-get
相关清理
sudo apt-get clean
(删除/var/cache/apt/archives/
)sudo apt-get autoremove
sudo rm -rf /tmp/*
(确保无重要文件在其中)find
命令排除关键路径,例如:sudo find /tmp -type f -mtime +7 -not -path "/tmp/important_dir/*" -delete
/
使用清理命令,优先针对/tmp
、/var/tmp
等临时目录。sudo
权限,执行前建议备份重要数据。systemd-tmpfiles
或tmpreaper
定时清理,减少手动操作风险。参考来源: