如何在linux下删除过时的txt文件作为bash脚本的一部分

时间:2013-06-23 07:37:19

标签: bash shell

我正在尝试编写一个脚本,该脚本是生成文件的较大脚本的一部分,这些文件会在这么多天后删除过时的备份。它们的格式为test-$(date +"%Y-%m-%d.txt")

这就是我所拥有的,这不是真的有效; find ~/cron/obnam -type f -mtime +3 | xargs rm>>$LOG-FILE 2>&1

这将在Linux下的Debian 7上使用。

2 个答案:

答案 0 :(得分:1)

不是100%准确,但也许这对你所描述的情况来说已经足够了:

find ~/cron/obnam -type f -mtime +3 -name 'test-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1

如果你有一些不能很好处理的角落案例,请发表评论,我会修改。

答案 1 :(得分:0)

一般性,我喜欢功能:

findtest() { find ~/cron/obname -type f -mtime +${1:-3} -name 'test-*.txt'; }

列出候选人。 e.g。

findtest      # default is 3 days
findtest  31  # say for 31 days

然后

trimtest () { findtest ${1:-7} | xargs rm -v ; }   # and executed

trimtest >> $LOGFILE

这种方式将行动(发现)政策(在N天后删除)和记录(附加到日志文件)分开。根据您的需要,您可能还会重写以传递整个标记,并允许较旧较新选项。

祝你好运。