温馨提示×

centos recycle怎么定时清理

小樊
36
2026-10-09 00:56:59
栏目: 智能运维

在 CentOS 里“recycle(回收站)”通常出现在两种场景:

  1. 桌面环境(GNOME/KDE)的回收站
  2. 自建的 recycle 目录(如脚本、程序删除文件时先移到 recycle)

下面分别说明定时清理方法。


一、清理桌面环境回收站(用户级)

CentOS 桌面回收站路径一般是:

~/.local/share/Trash/files

1️⃣ 使用 cron 定时清理(推荐)

例如:每天凌晨 3 点清理 7 天前的文件

crontab -e

添加:

0 3 * * * find /home/*/.local/share/Trash/files -type f -mtime +7 -delete

⚠️ 如果有多个用户,建议逐个用户处理,或用通配符(如上)


二、清理自建 recycle 目录(最常见)

如果你是自己做了类似:

mv file /data/recycle/

示例:每天清理 30 天前的文件

crontab -e
0 2 * * * find /data/recycle -type f -mtime +30 -delete

或连目录一起删:

0 2 * * * find /data/recycle -mtime +30 -exec rm -rf {} \;

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

1️⃣ 创建清理脚本

vim /usr/local/bin/clean_recycle.sh
#!/bin/bash
find /data/recycle -type f -mtime +30 -delete
chmod +x /usr/local/bin/clean_recycle.sh

2️⃣ 创建 service

vim /etc/systemd/system/clean-recycle.service
[Unit]
Description=Clean recycle dir

[Service]
Type=oneshot
ExecStart=/usr/local/bin/clean_recycle.sh

3️⃣ 创建 timer

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

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target

4️⃣ 启用

systemctl daemon-reload
systemctl enable --now clean-recycle.timer

四、常见参数说明

参数 含义
-mtime +7 7 天前
-mmin +60 60 分钟前
-type f 只删文件
-delete 直接删除

五、注意事项

✅ 先测试:

find /data/recycle -mtime +30

✅ 避免误删:

find /data/recycle -mtime +30 -print

✅ 重要数据不要放 recycle 长期保留


如果你能告诉我:

  • recycle 是系统自带还是自己建的
  • 路径在哪
  • 想保留几天

我可以直接帮你写一份可用配置。

0 踩