在 CentOS 里没有 Windows 那样的“回收站(Recycle Bin)”概念,但可以通过脚本 + 定时任务(cron)来模拟“回收站”并定时清理。下面给你几种常见做法。
mkdir -p /data/recycle
编辑 ~/.bashrc 或 /etc/bashrc:
alias rm='mv -t /data/recycle'
让 rm file 实际变成移动到回收站。
⚠ 注意:这只对交互 shell 有效,脚本里仍建议用
mv
crontab -e
加入:
0 0 * * * find /data/recycle -type f -mtime +7 -delete
0 0 * * * find /data/recycle -type d -empty -delete
创建服务:
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 拦截(谨慎)logrotate 管理日志类回收如果你说的是 某个具体软件(如 Samba / Nextcloud / 文件服务器)的 recycle,告诉我,我可以给你更精确的配置。