[Help] Cron to delete aged files?

Discussion in 'Programming' started by Hipto, Jan 8, 2010.

  1. #1
    I'm looking for a cron to automatically delete files which is like 3 hours after last modified, excluding htaccess and index.php.

    any help?
     
    Hipto, Jan 8, 2010 IP
  2. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This can be easily implemented using php.
     
    NeoCambell, Jan 9, 2010 IP
  3. Hipto

    Hipto Peon

    Messages:
    939
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OMG for goodness sake please don't post something useless
     
    Hipto, Jan 9, 2010 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    crontab automate task, it does not program by itself. you will need it to call up a script, preferably a shell/bash script.

    The following script can help attain mod time and access time, you can go from there.
    Then add an entry to cron.

    0 */3 * * * /path/to/script

    
    #!/bin/bash
    #Author: K.Sridhar
    #mod_acc.sh: script to find last modified and last accessed time of a file
    E_NOARG=65
    E_MOREARG=66
    if [  $# = 0 ]
    then
            echo -e '\E[31m' "Usage: $0 <filename>"
            tput sgr0
            exit $E_NOARG
    elif [ $# -gt 1 ]
    then
            echo -e '\E[31m' "Please give only one file at a time"
            tput sgr0
            exit $E_MOREARG
    fi
    LS=`which ls`
    MOD_TIME=`$LS -lart $1|cut -f6,7,8 -d" "`
    ACC_TIME=`$LS -laru $1|cut -f6,7,8 -d" "`
    echo "The modified time of the file is $MOD_TIME"
    echo "The accessed time of the file is $ACC_TIME"
    
    Code (markup):
     
    Kaizoku, Jan 9, 2010 IP
  5. Hipto

    Hipto Peon

    Messages:
    939
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanksm alot :) But i need to know how to delete i too :( I'm not very good
     
    Hipto, Jan 9, 2010 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    You can delete with following command, I suggest you get used to unix commands before doing anything with shell scripts, they can be dangerous. You might wipe out your entire filesystem.

    rm <file>
    Code (markup):
     
    Kaizoku, Jan 9, 2010 IP