Linux 回收站自动清理设置指南
一 桌面环境自带策略
二 命令行与定时任务
0 2 * * * /usr/bin/trash-empty
0 2 * * * /usr/bin/trash-empty --age 7d
0 2 * * * find ~/.local/share/Trash/files -mindepth 1 -mtime +7 -delete
0 2 * * * find ~/.local/share/Trash/info -mindepth 1 -mtime +7 -delete
三 systemd 定时器方式
[Unit]
Description=Trash Cleaner
[Service]
Type=oneshot
ExecStart=/usr/bin/trash-empty --age 7d
[Unit]
Description=Run Trash Cleaner daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now trash-cleaner.timer
systemctl list-timers trash-cleaner.timer
journalctl -u trash-cleaner.service
四 自定义回收站与别名
mkdir -p ~/.trash
alias rm='trash'
alias r='trash'
alias rl='ls ~/.trash'
alias ur='undelfile'
trash() { mv "$@" ~/.trash/; }
undelfile() {
mv -i ~/.trash/"$@" ./
}
cleartrash() {
read -p "clear sure? [n]" confirm
[[ $confirm =~ ^[Yy]$ ]] && /bin/rm -rf ~/.trash/*
}
0 2 * * * find ~/.trash -mindepth 1 -mtime +7 -delete
五 注意事项与排错