脚本删除30天以上的文件

Shell脚本不应该删除* root目录*下的任何文件。 我的path将会像/ export / home / ftp / …

我做了一些研究,找出了使用find和exec命令从特定path查找和删除超过30天的文件的方法。

* find / export / home / ftp / -type f -mtime +30 -exec rm -f {} \;

但根据要求,我只想从该目录中删除console.log和server.log,并排除其余的文件。

请帮我解决这个问题。

假设你真的需要使用find来recursion子目录:

 find /export/home/ftp \( -name console.log -or -name server.log \) -mtime +30 -exec rm -f {} + 

如果您只需要每个月删除旧的server.log和console.log,则还可以使用最有可能在RHEL下运行的logrotate 。 像这样的configuration片段可以在/etc/logrotate.d/*.conf或者configuration文件在系统上的任何地方工作。

 # rotate server.log and console.log every month # delete, not compress, old file /export/home/ftp/server.log /export/home/ftp/console.log { monthly rotate 0 } 

如上所述,一个自定义的月度cron也将运行良好。 实际上,因为logrotate是从cron运行的,所以可以认为这是一个cron类的扩展。 HTH。

为什么不使用每月的cron?

@monthly / usr / bin / rm -f console.log @monthly / usr / bin / rm -f server.log

这样做肯定会比较安全。

从debian tmpreaper检查这个方便的工具

另一种方法是使用xargs,这显然更有效 – http://www.sunmanagers.org/pipermail/summaries/2005-March/006255.html

所以你可以做这样的事情:

 find /export/home/ftp -maxdepth 1 \( -name console.log -or -name server.log \) -mtime +30 | xargs -O -r rm