shell script

Discussion in 'Programming/Scripts' started by Nnyan, Jun 22, 2006.

  1. Nnyan

    Nnyan New Member


    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
  2. New Member

    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!)
    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 ;-).
  3. Nnyan

    Nnyan New Member,

    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?
  4. Nnyan

    Nnyan New Member

    Ok when i enter this line;

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

    I get this error:

    find: invalid predicate `-delete'
  5. falko

    falko Super Moderator ISPConfig Developer

    Have a look at
    man find
    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 -delete
    Instead 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.

Share This Page