shell脚本定时删除文件

项目中需要定时清理文件夹中的过期文件(本篇文章指定3天前即为过期),百度了几篇博文记录下实现过程
这篇博文是在之后写的,当时的参考博文就没有再找了,有需要的小伙伴可以自行搜索
1.编辑shell脚本,删除指定文件夹下的文件,脚本如下:
vi delete.sh

#! /bin/bash
data_path="/home/test01"
expire_time=3
function(){
	local currentDate = `date+%s`
	echo "current date is:" $currentDate
	for file in `find $1 -name "*.txt"`
	do
	local name=$file
	local modifyDate=$(stat -c %Y $file)
	local existTime=$[$currentDate-$modifyDate]
	existTime=$[$existTime/86400]
	if[ $existTime -gt $exist_time ];then
	echo "file:" $name "modify Date:" $modifyDate + "Exist time:" $existTime + "Delete:yes"
	rm -rf $file
	else
	echo "file:" $name "modify Date:" $modifyDate + "Exist time:" $existTime + "Delete:no"
	fi
	done
}
deletefiles $data_path

2.用crontab命令编辑定时任务,执行以上脚本
输入命令crontab -e进入编辑模式

0 0 * * * sh /aom/delete.shell

每天的00:00执行一次删除脚本

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值