linux系统 shell脚本 定时删除三个月之前的所有文件夹和文件

1、创建shell脚本:

#!/bin/bash
echo "========The script excute begin========"
baseFolder="/apps/tomcat8080/apache-tomcat-8.0.45/webapps/abnormalImg/"
year=$(date +%Y)
month=`date +%m`
monthStr=${month:0:1}
if [ $monthStr -eq "0"  ];then
 month1=${month#*0}
else
 month1=$month
fi
month2=`expr $month1 - 2`
cd $baseFolder
for dir in $(ls $baseFolder)
do
 if [ -d $dir ];then
  if [ $dir != $year ];then
   echo "The folder "$dir" will be removed"
   rm -rf $dir
  else folder=$baseFolder$year"/"
  fi
 fi
done
cd $folder
for dir1 in $(ls $folder)
do
 if [ -d $dir1 ];then
  if [ $dir1 -lt $month2 ];then
   echo "The folder "$dir1" will be removed"
   rm -rf $dir1
  fi
 fi
done
echo "========The script excute end========"

上面这个脚本的大概意思是:获取当年的年份 year,当月的月份 month ;然后遍历图片文件夹中所有目录,判断是不是小于当月的前三个月,如果是就直接 rm -rf 删除掉,如果不是则保留;

2、创建crontab 定时任务:

    输入 crontab -e 进入定时任务创建模式,然后编写下面的定时任务:意思是每天早上8:10执行指定目录下面的指定.sh脚本文件(这里指的就是第一步中创建的定时删除文件的脚本文件 test_delete.sh):

10 8 * * * /apps/tomcat8080/apache-tomcat-8.0.45/webapps/test_delete.sh;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值