0

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.

This_Is_Fun
  • 124
  • 1
  • 2
  • 11
  • 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. – user1686 Feb 01 '11 at 12:44

1 Answers1

5

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.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • Glad to get the improved version; Also confirmed it works as a cron job :o) – This_Is_Fun Feb 01 '11 at 09:46
  • 1
    Recent `find` versions support `-delete`. – user1686 Feb 01 '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 01 '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 02 '11 at 09:26