Ubuntu設置回收站有效期的常用方法
Ubuntu系統默認的回收站(Trash)功能沒有直接提供“有效期”設置選項,但可以通過以下幾種方式實現自動清理過期文件的目標:
步驟1:安裝trash-cli
打開終端,運行以下命令安裝trash-cli(命令行回收站管理工具):
sudo apt update && sudo apt install trash-cli
步驟2:配置有效期
.trashrc配置文件(用于存儲trash-cli的個人設置):nano ~/.trashrc
7替換為你需要的有效期天數(如30天則表示文件保留30天):[trash]
expire_days = 7
Ctrl+X→Y→Enter。步驟3:驗證配置
test.txt),觀察其是否進入回收站;ls ~/.local/share/Trash/files命令,若文件已被自動刪除,則配置生效。若不想安裝額外工具,可通過cron定時運行清理腳本,定期刪除回收站中的舊文件。
步驟1:創建清理腳本
empty_trash.sh):nano ~/empty_trash.sh
#!/bin/bash
# 計算7天前的時間戳(秒)
SEVEN_DAYS_AGO=$(( $(date +%s) - 7*24*60*60 ))
# 刪除回收站中超過7天的文件
find ~/.local/share/Trash/files -type f -mtime +7 -exec rm -f {} \;
# 可選:同時刪除對應的info文件(記錄文件信息的目錄)
find ~/.local/share/Trash/info -type f -mtime +7 -exec rm -f {} \;
chmod +x ~/empty_trash.sh
步驟2:設置cron定時任務
crontab -e編輯當前用戶的cron任務;0 2 * * * ~/empty_trash.sh
部分Ubuntu版本(如18.04及以上)的GNOME桌面環境提供了圖形化的回收站自動清理設置: