Jeff
Jeff

Reputation: 245

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

I have to delete all the pdf files which are more than 30 days old at 11:30 PM

The below given are the two working cronjobs in /etc/crontab

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 {} \;

I would like to know which one is better among the two and the reason.

Please help.

Upvotes: 0

Views: 46

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

The answer is probably option 3. Use -delete.

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

Both the options in the question spawn sub-shells to do the deletion work which this option avoids entirely.

The portability of -delete is somewhat limited. GNU find supports it as does FreeBSD find (at least according to this man page) but OpenBSD find doesn't appear to. I don't know about any others.

As user3159253 says in their comment among the first two options the first is likely somewhat faster due to requiring fewer invocations of rm but is not safe for filenames with newlines in them (and possibly a handful of other characters but I'm not sure).

A modification of the second option to use -exec rm {} \+ will be better as well as it will also reduce the number of invocations of rm by giving it multiple files at once and may or may not be better than the first option at that point but will still not beat the option given here.

Upvotes: 4

Related Questions