在 CentOS 里,你说的 “recycle” 一般指的是 回收站(用户级或系统级),常见有两种场景:
/data/recycle)下面分别说明如何定时清理。
~/.local/share/Trash
结构:
Trash/
├── files/ # 删除的文件
└── info/ # 删除信息
例如:每天凌晨 3 点清理 7 天前的文件
crontab -e
添加:
0 3 * * * find ~/.local/share/Trash/files -type f -mtime +7 -delete
0 3 * * * find ~/.local/share/Trash/info -type f -mtime +7 -delete
假设你把删除的文件统一放到:
/data/recycle
mv file /data/recycle/
例如:每天清理 30 天前的文件
crontab -e
0 2 * * * find /data/recycle -type f -mtime +30 -delete
0 2 * * * find /data/recycle -type d -empty -delete
vi /usr/local/bin/clean_recycle.sh
#!/bin/bash
find /data/recycle -type f -mtime +30 -delete
find /data/recycle -type d -empty -delete
chmod +x /usr/local/bin/clean_recycle.sh
vi /etc/systemd/system/clean-recycle.service
[Unit]
Description=Clean recycle bin
[Service]
Type=oneshot
ExecStart=/usr/local/bin/clean_recycle.sh
vi /etc/systemd/system/clean-recycle.timer
[Unit]
Description=Daily clean recycle
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
systemctl enable --now clean-recycle.timer
查看:
systemctl list-timers
rm -rf 直接清根目录-print 测试:find /data/recycle -mtime +30 -print
| 需求 | 命令 |
|---|---|
| 清理 7 天前 | -mtime +7 |
| 清理 30 天前 | -mtime +30 |
| 只删文件 | -type f |
| 只删空目录 | -type d -empty |
如果你能告诉我:
我可以直接给你一套可直接用的配置。