Dido
Dido

Reputation: 59

Cron job to delete files created before a specific time

I have this cron job to empty a temp folder I have once a day:

rm -rf /home/username/public_html/temp/*

I want this cron job to delete all files which created before the it will run with 5 minutes and up just to make sure that my script don't need these files anymore.

Let's say I set the cron job to run everyday at 10:00 AM .. I want it to delete all files in this folder which created before 09:55 AM

Thank you so much experts!

Upvotes: 3

Views: 4799

Answers (3)

Benubird
Benubird

Reputation: 19487

Similar to Jeffreys answer, I'd suggest something like this:

for i in `find /home/username/public_html/temp -type f -mmin +5`
    do rm $i
done

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86386

Alternate solution would be:-

point cron job to execute a php file once a day.

 $dir = 'your/directory/';
  foreach(glob($dir.'*.*') as $v){
  $last_modified = filemtime($v);//get the last modified time of file
  $fmtime=date("h:i:s", $last_modified);
  $currentTime=date("h:i:s");

  if (//check here if your file is created before 09:55Am)
  unlink($v);

  }

Thanks.

Upvotes: 0

Jeffrey Hantin
Jeffrey Hantin

Reputation: 36494

If you're using GNU find, try this:

find /home/username/public_html/temp -type f -mmin +5 -delete

Should also work with FreeBSD or many other versions of find.

Upvotes: 2

Related Questions