温馨提示×

centos recycle如何定时删除

小樊
37
2026-10-23 16:28:23
栏目: 智能运维

你提到的 “centos recycle”,通常是指 CentOS 中文件删除后进入的回收站目录(常见是 .Trash 或某些服务如 Samba、Nextcloud、自建回收站脚本产生的 recycle 目录)。
下面按常见场景给你几种定时删除方法。


一、如果是 Samba 的 recycle 回收站

Samba 自带 recycle 模块,可直接在配置里设置自动清理。

1. 编辑 Samba 配置

vi /etc/samba/smb.conf

2. 在对应共享中添加

[share]
path = /data/share
read only = no
recycle:repository = .recycle
recycle:keeptree = yes
recycle:versions = yes
recycle:maxsize = 0
recycle:exclude = *.tmp *.log
recycle:touch = yes

3. 自动删除旧文件(推荐用 cron)

crontab -e

每天凌晨清理 7 天前的文件:

0 0 * * * find /data/share/.recycle -type f -mtime +7 -delete

二、如果是系统/用户自建 recycle 目录

比如:

/root/recycle
/home/user/recycle

使用 cron + find 定时删除

crontab -e

每天删除 30 天前文件

0 2 * * * find /path/to/recycle -type f -mtime +30 -delete

删除空目录

0 2 * * * find /path/to/recycle -type d -empty -delete

三、使用 systemd timer(CentOS 7+ 推荐)

比 cron 更现代。

1. 创建服务

vi /etc/systemd/system/clean-recycle.service
[Unit]
Description=Clean recycle bin

[Service]
Type=oneshot
ExecStart=/usr/bin/find /path/to/recycle -type f -mtime +7 -delete

2. 创建定时器

vi /etc/systemd/system/clean-recycle.timer
[Unit]
Description=Daily clean recycle

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

3. 启用

systemctl enable clean-recycle.timer
systemctl start clean-recycle.timer

四、安全建议

  • 先用 -print 测试,再 -delete
find /path/to/recycle -mtime +7 -print
  • 不要直接删 / 或 Home
  • 可加日志:
>> /var/log/recycle-clean.log 2>&1

如果你能告诉我:

  • recycle 是 Samba / Nextcloud / 自建脚本
  • 具体路径

我可以给你精确命令。

0 踩