PDA

View Full Version : shell script


Nnyan
23rd June 2006, 00:18
Hello,

Sorry for the noob question could not find an answer for this.

I need a shell script that I can run in cron that will search one or more folders for specified file(s) and delete them if they are older then (ex: ) 10 days.

Thank you

geek.de.nz
23rd June 2006, 08:06
Look up the documentation for find:

man find

You can for example search for files that were accessed (read) 10 days ago or more with:

find . -name '*tmp*' -atime +9

(Note that it's 9 not 10!)
With

find . -name '*tmp*' -atime +9 -delete

You delete them.

You can just put that line directly into the cron or in a shell script if you have to.

Note: "." is the current working directory. If you want to change it do so ;-).

Nnyan
23rd June 2006, 19:09
geek.de.nz,

Thank you for the info, never knew you could put something like this right in the cron! I'm guessing I would have to put the complete path since I don't know what the current working directory would be for cron (if i put this right inside the cron).

You've got me curious now can you actually run scripts inside cron? Are there any limitations to this?

Nnyan
23rd June 2006, 22:51
Ok when i enter this line;

find . -name '*.tar.gz' -atime +9 -delete

I get this error:

find: invalid predicate `-delete'

falko
24th June 2006, 14:47
Have a look at man find
geek.de.nz,

Thank you for the info, never knew you could put something like this right in the cron! I'm guessing I would have to put the complete path since I don't know what the current working directory would be for cron (if i put this right inside the cron).

You've got me curious now can you actually run scripts inside cron? Are there any limitations to this?
To create a cron job that runs your tasl at 05:00h each day (for example), run
crontab -e (as root) and enter this:
0 5 * * * find . -name '*tmp*' -atime +9 -deleteInstead of just find you can use the full path to find, e.g. /usr/bin/find. Run which find to find out where find is on your system.