1

I need to set up a job to delete all of the regular files in the /home/admin directory on the second day of every month at 8:30 A.M. It seems like wrong command:

# crontab -e
30 08 02 * /bin/find /home/admin -type f -exec /bin/rm {} ";"
2

The crontab(5) manual specifies five time fields:

 field         allowed values
 -----         --------------
 minute        0-59
 hour          0-23
 day of month  1-31
 month         1-12 (or names, see below)
 day of week   0-7 (0 or 7 is Sun, or use names)

Your example only has four.

This would make the cron job run correctly:

30 8 2 * * /bin/find /home/admin -type f -exec /bin/rm {} ";"

For a more efficient and quicker cleanup:

30 8 2 * * /bin/find /home/admin -type f -print0 | xargs -0 rm
1
  • There's also GNU find's -delete option. May 31 '15 at 14:38
1

You need to add 30 08 02 * * and \;

# crontab -e
30 08 02 * * /bin/find /home/admin -type f -exec /bin/rm {} \;

Now it will work.

2
  • 1
    Notice that there nothing wrong with using ";" at the end of the command in when using -exec in find. It's a character longer, but IMHO looks nicer in a script.
    – Kusalananda
    May 31 '15 at 12:31
  • 1
    Using a + instead of the quoted semicolon has its merits. It means that find will execute the command (rm in this case) with multiple file names each time. May 31 '15 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy