Cron 作业删除在特定时间之前创建的文件

发布于 2024-10-05 18:20:49 字数 253 浏览 0 评论 0原文

我有这个 cron 作业来清空我每天一次的临时文件夹:

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

我希望这个 cron 作业删除在它运行 5 分钟及以上之前创建的所有文件,只是为了确保我的脚本不需要这些文件不再了。

假设我将 cron 作业设置为每天上午 10:00 运行..我希望它删除此文件夹中上午 09:55 之前创建的所有文件。

非常感谢专家!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失而复得 2024-10-12 18:20:49

如果您使用的是 GNU find,请尝试以下操作:

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

也应该适用于 FreeBSD 或许多其他版本的 find

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.

焚却相思 2024-10-12 18:20:49

替代解决方案是:-

指定 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);

  }

谢谢。

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.

呆萌少年 2024-10-12 18:20:49

与杰弗里的回答类似,我建议这样:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文