温馨提示×

linux recycle怎么自动清理

小樊
43
2026-10-05 13:21:15
栏目: 智能运维

在 Linux 里**“回收站(recycle)”自动清理**通常分几种场景,下面按常见情况说明怎么做。


一、桌面环境自带的回收站(Trash)

如果你用的是 GNOME / KDE / XFCE 等桌面环境:

1. GNOME(常见 Ubuntu 默认)

GNOME 没有内置自动清理回收站的功能,需要手动或用脚本。

方法:用 systemd 定时清理

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 天前的文件自动删除


二、服务器 / 命令行环境(无桌面)

常见做法是:

  • 删除文件先放到 ~/trash
  • 定时清理

示例:自动清理 30 天前的回收文件

find ~/trash -type f -mtime +30 -delete
find ~/trash -type d -empty -delete

加到 crontab:

crontab -e
0 3 * * * find ~/trash -mtime +30 -delete

三、使用现成工具(推荐)

1. 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 天数

如果你能告诉我:

  • 是 服务器还是桌面
  • 用的是 哪个 Linux 发行版
  • 是想清理 系统回收站还是自己定义的目录

我可以给你一套直接能用的配置。

0 踩