How do I setup a cronjob to auto-delete files in a directory?

Discussion in 'Site & Server Administration' started by 408nate08, Mar 25, 2011.

  1. #1
    I'm looking to setup a cronjob in order to automatically delete files within a certain directory 24 hours after it's been uploaded. I've seen some tutorials on this, but I'm misunderstanding what they're saying and I don't want to mess anything up.

    Here is what I'm seeing:

    [​IMG]

    In order to delete a file that's been created 24 hours ago, what should the time settings be? I believe this is the command that I should put, but can someone verify this please?

    find /path/to/files -mtime +1 -exec rm  {}\;
    Code (markup):
    Thank you!

    - Nate :confused:
     
    408nate08, Mar 25, 2011 IP
  2. st1905

    st1905 Well-Known Member

    Messages:
    573
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    135
    #2
    You can use php to do this

    <?php
    
    $files = glob("/directory/you/want/to/delete/files/in/*.*");
    
    foreach($files as $file) unlink($file);
    
    ?>
    Code (markup):
    save above as cron.php and modify directory.

    and in crontab

    0 0 * * * /usr/local/bin/php /diretcory/where/your/cron.php

    minute = 0

    hour = 0
    day = *
    month=*
    weekday=*

    This will run everyday at midnight.
     
    st1905, Mar 26, 2011 IP
  3. 408nate08

    408nate08 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks "st1905" for the reply. :)

    It's just that I need the file to delete exactly (or slightly around) 24 hours after it was uploaded. For instance, if a file is uploaded at 7:30PM a certain day, I want it deleted the next day at 7:30PM. I'm not sure if I can achieve that with a cronjob, though. :confused:
     
    408nate08, Mar 26, 2011 IP