我正在尝试编写一个脚本,它是生成文件的更大脚本的一部分,它会在这么多天后删除过时的备份。它们的形式为test-$(date +"%Y-%m-%d.txt")
这就是我所拥有的,它并没有真正起作用;find ~/cron/obnam -type f -mtime +3 | xargs rm>>$LOG-FILE 2>&1
这将在 Linux 下的 Debian 7 上使用。
不是 100% 准确,但对于您描述的情况,这可能已经足够了:
find ~/cron/obnam -type f -mtime +3 -name 'test-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1
如果您有一些不能很好处理的极端情况,请发表评论,我会修改。
一般来说,我喜欢功能:
findtest() { find ~/cron/obname -type f -mtime +${1:-3} -name 'test-*.txt'; }
列出候选人。例如
findtest # default is 3 days
findtest 31 # say for 31 days
然后
trimtest () { findtest ${1:-7} | xargs rm -v ; } # and executed
trimtest >> $LOGFILE
这种方式将操作(发现)策略(N 天后删除)和记录(附加到日志文件)分开。根据您的需要,您还可以重写以传递整个标志,并允许较旧或较新的选项。
祝你好运。