以下是Linux清理系統臨時文件夾的常用方法:
手動刪除
rm
命令直接刪除臨時文件/目錄(謹慎操作,避免誤刪):sudo rm -rf /tmp/* # 清空/tmp目錄
sudo rm -rf /var/tmp/* # 清空/var/tmp目錄
使用系統工具
sudo systemd-tmpfiles --clean # 立即清理/tmp和/var/tmp
可通過編輯/etc/tmpfiles.d/
下的配置文件設置自動清理規則(如按時間刪除文件)。定時任務(Cron)
clean_tmp.sh
)并添加到crontab
,定期清理臨時文件:# 示例腳本內容(清理/tmp中超過7天的文件)
#!/bin/bash
find /tmp -type f -mtime +7 -exec rm -f {} \;
添加定時任務:crontab -e
# 添加行:0 2 * * * /path/to/clean_tmp.sh # 每天凌晨2點執行
第三方工具
sudo apt-get install bleachbit # Debian/Ubuntu
sudo yum install bleachbit # CentOS/RHEL
sudo bleachbit # 運行后選擇“臨時文件”清理
注意事項:
/tmp
目錄通常在系統重啟后自動清理,部分發行版可通過修改/etc/default/rcS
中TMPTIME
參數調整啟動清理策略。