Some commands I use (i.e rsync) work fine in cron jobs; Will this:

find /path/to/files* -mtime +30 -exec rm {} \;

...or do I need to put it into a file? I can test it myself soon, however asking may save precious time.

link|improve this question

To answer "...or do I need to put it into a file": cron uses system() to execute cronjobs, which gives the entire line to the sh shell (for example, /bin/sh -c 'find ....'). In other words, if a command can be used inside sh, it can be used in a cronjob. – grawity Feb 1 '11 at 12:44
feedback

1 Answer

up vote 5 down vote accepted

No, this will not work. You can't give a wildcard to specify the place where to search. Use the -name parameter instead, like this:

find /path/to/files -name "*" -mtime +30 -print0 | xargs -0 rm

I also made sure this command can handle lot's of files and files with spaces in it's name via the use of xargs instead of -exec.

link|improve this answer
Glad to get the improved version; Also confirmed it works as a cron job :o) – This_Is_Fun Feb 1 '11 at 9:46
1  
Recent find versions support -delete. – grawity Feb 1 '11 at 12:44
-name "*" can be removed since it's implied. Recent versions of find support -exec foo {} + which gives similar results to using xargs (but -delete is all you need as grawity says). – Dennis Williamson Feb 1 '11 at 14:35
Haven't tested the new comments yet / (remove -name "*") / (add -delete) / no 'xargs' or 'rm' >> find /path/to/files -mtime +30 -delete >>> is that right? – This_Is_Fun Feb 2 '11 at 9:26
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.