Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|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
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.

share|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
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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