在 Unix 中使用 shell 脚本删除超过 10 天的文件

编程入门 行业动态 更新时间:2024-10-28 12:23:43
本文介绍了在 Unix 中使用 shell 脚本删除超过 10 天的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 shell 脚本的新手,有人可以帮忙吗?我想将文件夹中的脚本从当前日期删除回 10 天.脚本如下所示:

I'm new to shell scripts, can anyone help? I want to delete scripts in a folder from the current date back to 10 days. The scripts looks like:

2012.11.21.09_33_52.script 2012.11.21.09_33_56.script 2012.11.21.09_33_59.script

脚本将使用 Crontab 每 10 天运行一次,这就是我需要当前日期的原因.

The script will run in every 10 day with Crontab, that's why I need the current date.

推荐答案

find 是此类任务的常用工具:

find is the common tool for this kind of task :

find ./my_dir -mtime +10 -type f -delete

说明

  • ./my_dir 您的目录(替换为您自己的)
  • -mtime +10 超过 10 天
  • -type f 仅文件
  • -delete 并不奇怪.在执行整个命令之前删除它以测试您的 find 过滤器
  • ./my_dir your directory (replace with your own)
  • -mtime +10 older than 10 days
  • -type f only files
  • -delete no surprise. Remove it to test your find filter before executing the whole command

并注意 ./my_dir 的存在以避免意外!

And take care that ./my_dir exists to avoid bad surprises !

更多推荐

在 Unix 中使用 shell 脚本删除超过 10 天的文件

本文发布于:2023-10-08 23:52:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   文件   Unix   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!