两个 cron 在 linux 做同样的工作,哪个更好

Two crons doing same job in linux, which one is better

我必须在 11:30 下午删除所有超过 30 天的 pdf 文件

下面给出的是 /etc/crontab

中的两个工作 cronjobs
30 23 * * * root find /var/www/html/site/reports/ -name ".pdf" -type f -mtime +30 | xargs -I {} rm -f {} \;

30 23 * * * root find /var/www/html/site/reports/ ( -name ".pdf" ) -type f -mtime +30 -exec rm {} \;

我想知道这两个哪个更好,以及原因。

请帮忙。

答案可能是选项3。使用-delete

30 23 * * * root find /var/www/html/site/reports/ -name '*.pdf' -type f -mtime +30 -delete

问题中的两个选项都会生成子 shell 来执行此选项完全避免的删除工作。

-delete 的可移植性有些受限。 GNU find 和 FreeBSD find 一样支持它(至少根据 this man page),但 OpenBSD find 似乎不支持。我不知道其他人。

正如 user3159253 在他们对前两个选项的评论中所说,第一个选项可能更快一些,因为需要更少的 rm 调用,但对于其中包含换行符的文件名(可能还有少数其他选项)不安全字符,但我不确定)。

修改第二个选项以使用 -exec rm {} \+ 会更好,同时它也会通过一次给它多个文件来减少 rm 的调用次数,可能会也可能不会在这一点上比第一个选项更好,但仍然不会超过此处给出的选项。