Web Hosting Talk







View Full Version : Deleting file script?


crspyjohn
01-30-2005, 05:51 PM
Is there a script if i use cronjob to run it, it will delete files in a folder only if the file is x days old?

runesolutions
01-30-2005, 06:55 PM
I used something similar to the following, which might give you a start:


#!/bin/sh
dateago=`date -I -d '1 day ago'`
deldate=`echo $dateago | tr -d "-"`
dirdel="testdir"

echo "Deleting anything older than $deldate in '$dirdel'."

for i in `ls $dirdel`; do
dirdate=`echo $i | tr -d "-"`
if [ "$dirdate" -lt "deldate" ]; then
rm -f $dirdel/$i
fi
done


But check it carefully. I used to use this on a Linux box and I can't test it now as the 'date' command seems to take different parameters on FreeBSD.

crspyjohn
01-30-2005, 06:57 PM
dateago=`date -I -d '1 day ago'`
deldate=`echo $dateago | tr -d "-"`
dirdel="testdir"

would i edit these parts?

runesolutions
01-30-2005, 06:59 PM
Yep, change dirdel to the path to the directory you want to delete things in and dateago to the date you wish to delete from (the example just uses things over 1 day old).

crspyjohn
01-30-2005, 07:16 PM
dateago=`date -I -d '30 day ago'`
deldate=`echo $dateago | tr -d "-"`
dirdel="/home/user/www/file/"

would this be correct for 30 days?

runesolutions
01-30-2005, 07:25 PM
I think it's '30 days ago'.

Try doing a man date at the shell and that should help with the 'date' command.