博客
关于我
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/

你可能感兴趣的文章
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
node+express+mysql 实现登陆注册
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>