博客
关于我
linux定时删除过期文件
阅读量:422 次
发布时间:2019-03-06

本文共 609 字,大约阅读时间需要 2 分钟。

需求说明

每日凌晨0点定时删除/temp目录下的所有一个月未被访问的文件。

脚本实现

linux 终端输入crontab -e,添加定时任务脚本命令

[root@localhost ~]#  crontab -e

在文件末尾追加

0 0 * * * find /temp -atime +30 -exec rm -rf {} \;

参数说明

命令格式:

find 对应目录 -name "文件名" -type f -mtime +n -exec rm -rf {} ;

  • -type

    f:普通文件

    d:目录

  • -mtime

    修改时间(modify time)

  • -atime

    访问时间(access time)

  • -ctime

    状态变更时间(change time)

  • n

    +n 第n天之前的,不包括第n天当天

    -n 第n天到今天的,不包括第n天当天

    备注: n为整数,以天为单位,0x24表示今天,1x24表示昨天;n有一位小数,以小时为单位,如0.5x24;n有两位小数,以分钟为单位,如0.55x24;

脚本验证

查看定时任务是否被加入定时任务列表

[root@localhost ~]#  crontab -l

查看文件是否被删除,这里调整为在13点25分删除1天内创建的文件(删除刚创建的文件),测试用,操作小心哦

25 13 * * * find /temp -mtime -1 -exec rm -rf {} \;

转载地址:http://saxuz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>