温馨提示×

centos recycle如何定时

小樊
39
2026-10-14 22:09:26
栏目: 智能运维

在 CentOS 里没有 Windows 那样的“回收站(Recycle Bin)”概念,但可以通过脚本 + 定时任务(cron)来模拟“回收站”并定时清理。下面给你几种常见做法。


一、模拟回收站(推荐做法)

1. 建立回收站目录

mkdir -p /data/recycle

2. 替换 rm 命令(可选)

编辑 ~/.bashrc 或 /etc/bashrc:

alias rm='mv -t /data/recycle'

让 rm file 实际变成移动到回收站。

⚠ 注意:这只对交互 shell 有效,脚本里仍建议用 mv


二、定时清理回收站(核心)

方法 1:用 cron 定时清理(最常用)

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

crontab -e

加入:

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

清理空目录

0 0 * * * find /data/recycle -type d -empty -delete

方法 2:systemd timer(CentOS 7+)

创建服务:

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

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

创建定时器:

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

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

启用:

systemctl enable --now recycle-clean.timer

三、常见清理策略示例

需求 命令
保留 3 天 find /data/recycle -mtime +3 -delete
只删日志 find /data/recycle -name "*.log" -mtime +1 -delete
每周日清理 0 2 * * 0 find /data/recycle -mtime +7 -delete

四、注意事项

  • rm -rf 不会被 alias 拦截(谨慎)
  • 生产环境建议 只 mv,不真删
  • 可用 logrotate 管理日志类回收

如果你说的是 某个具体软件(如 Samba / Nextcloud / 文件服务器)的 recycle,告诉我,我可以给你更精确的配置。

0 踩