Cron task to delete files created before a certain time

I have this cron job to clear the temporary folder that I have once a day:

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

I want this cron job to delete all files created before it works with 5 minutes and only so that my scripts no longer need these files.

Let's say I set the cron task to work every day at 10:00. I want him to delete all files in this folder created before 09:55

Thank you very much!

+3
source share
3 answers

If you are using GNU find, try the following:

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

It should also work with FreeBSD or many other versions find.

+2

: -

cron php .

 $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);

  }

.

0

, - :

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

Source: https://habr.com/ru/post/1778045/


All Articles