I need to remove files older than 3 days with a cron job in 3 different directories. (these 3 directories are children of a parent directory /a/b/c/1
& /a/b/c/2
& /a/b/c/3
) Can this be done with one line in the crontab?
Shell Script – Cron Job to Delete Files Older Than 3 Days
cronfilesrmshell-scripttimestamps
Best Answer
This is easy enough (although note that this goes by a modification time more than 3 days ago since a creation time is only available on certain filesystems with special tools):
Remove the
#
before the-delete
once you are sure that it is finding the files you want to remove.To have it run by cron, I would probably just create an executable script (add a shebang -
#!bin/sh
to the top line of the file and make executable withchmod a+x
), then put it in an appropriatecron
directory like/etc/cron.daily
or/etc/cron.weekly
. Provided of course that you do not need a more specific schedule and that these directories exist on your distro.Update
As noted below, the
-delete
option forfind
isn't very portable. A POSIX compatible approach would be:Again remove the
#
when you are sure you have the right files.Update2
To quote from Stéphane Chazelas comment below: