在 Linux 里**“回收站(recycle)”自动清理**通常分几种场景,下面按常见情况说明怎么做。
如果你用的是 GNOME / KDE / XFCE 等桌面环境:
GNOME 没有内置自动清理回收站的功能,需要手动或用脚本。
mkdir -p ~/.config/systemd/user
创建服务文件:
~/.config/systemd/user/trash-clean.service
[Unit]
Description=Clean Trash
[Service]
Type=oneshot
ExecStart=/usr/bin/find /home/$USER/.local/share/Trash -mtime +7 -delete
创建定时器:
~/.config/systemd/user/trash-clean.timer
[Unit]
Description=Daily Trash Clean
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=default.target
启用:
systemctl --user daemon-reload
systemctl --user enable --now trash-clean.timer
表示 7 天前的文件自动删除
常见做法是:
~/trashfind ~/trash -type f -mtime +30 -delete
find ~/trash -type d -empty -delete
加到 crontab:
crontab -e
0 3 * * * find ~/trash -mtime +30 -delete
trash-cli(最常用)pip install trash-cli
自动清理(例如 30 天):
trash-empty 30
加入 cron:
0 2 * * * /usr/bin/trash-empty 30
比如回收站超过 5G 就清理最旧的:
du -sh ~/.local/share/Trash
可写脚本判断 du 结果后删除。
| 场景 | 推荐方式 |
|---|---|
| 桌面用户 | trash-cli + cron |
| 服务器 | 自定义 trash + find |
| 想省事 | trash-empty 天数 |
如果你能告诉我:
我可以给你一套直接能用的配置。