Linux保留最近30天文件的shell脚本

#!/bin/bash

# 当前时间
current_time=`date  +"%Y-%m-%d %H:%M:%S"`
# 保留日志的天数
retain_log_days=30
retain_days_ago_date=`date -d "${retain_log_days} day ago" +%Y%m%d`

# 清理的目录集合:须以/结尾
clean_dirs=('/data/bootstart/service/izhaowo_city_expansion_manage/logs/' '/data/bootstart/service/izhaowo_wedding_planning_operation_center_manage/logs/' '/data/service/manage/manage_partner/logs/' '/data/service/manage/worker-excitaion/logs/' '/data/service/manage/aijiuxi-bus/logs/')

echo "日志清理开始[$current_time],清理[$retain_days_ago_date]之前的日志..."

# 遍历清理
for dir in ${clean_dirs[*]}
do
  echo "开始清理目录[$dir]..."
  files=`ls $dir -lrt --time-style="+%Y%m%d" | sed "1 d"`
  # 重置换行符
  IFS_OLD=$IFS
  IFS=$'\n'
  for file in ${files[*]}
  do
    IFS_NEW=$IFS
    IFS=" "
    file_date=`echo ${file} | awk '{print $6}'`
    file_name=`echo ${file} | awk '{print $7}'`
    if [ $file_date -le $retain_days_ago_date ]
    then
      echo "${dir}${file_name} 应该删除..."
      `rm -rf ${dir}${file_name}`
    fi
    IFS=$IFS_NEW
  done
  # 恢复换行符
  IFS=$IFS_OLD
done

echo "日志清理结束..."

结合crontab或supervisor使用,即可实现文件的定时清理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值