Linux脚本根据文件大小删除,Shell脚本扫描文件系统,根据指定条件删除文件(根据文件大小删除)...

因程序执行过程中生成大量的日志,导致日志文件迅速增大,占用大量磁盘空间,影响到程序正常运行,程序执行速度减慢

在指定文件中保存目录路径,并指定删除条件(如:大小值,特定字符串正则表达式、日期正则表达式),运行脚本读取该文件,若符合条件,则删除该文件。

• 实现:1.根据文件大小删除文件,运行脚本,大于指定值的文件被删除

脚本实现代码如下:

#!/bin/bash

ParamSize=$2

defaultSizeByte=1073741824

############################################################################################

##################################获取文件大小##############################################

function getFileSizeByte(){

fileSize=`du -sh $file_array|awk '{print $1}'`

str=${fileSize:0-1:1}

num=${fileSize%%$str*}

if [ "$str" == "G" ]

then

fileSizeByte=`echo "$num*1024*1024*1024"|bc`

fi

if [ "$str" == "M" ]

then

fileSizeByte=`echo "$num*1024*1024"|bc`

fi

if [ "$str" == "K" ]

then

fileSizeByte=`echo "$num*1024"|bc`

fi

}

############################################################################################

##################################获取参数值Byte大小########################################

function getParamSizeByte(){

if [ ! -z "$ParamSize" ];

then

strParam=${ParamSize:0-1:1}

numParam=${ParamSize%%$strParam*}

if [ "$strParam" == "G" ]

then

numParamByte=`echo "$numParam*1024*1024*1024"|bc`

fi

if [ "$strParam" == "M" ]

then

numParamByte=`echo "$numParam*1024*1024"|bc`

fi

if [ "$strParam" == "K" ]

then

numParamByte=`echo "$numParam*1024"|bc`

fi

elif [ ! -n "$ParamSize" ]

then

numParamByte=$defaultSizeByte

fi

}

##############################################################################################

##################################查找并删除文件##############################################

function scanAndDelFile(){

local cur_dir parent_dir workdir

file_array=()

workdir=$1

cd ${workdir}

if [ ${workdir} = "/" ]

then

cur_dir=""

else

cur_dir=$(pwd)

fi

for dirlist in $(ls ${cur_dir})

do

if test -d ${dirlist};then

cd ${dirlist}

scandir ${cur_dir}/${dirlist}

cd ..

else

echo ${cur_dir}/${dirlist}

fileCount=`ls -l |grep "^-"|wc -l`

for file in ${cur_dir}/${dirlist}

do

for ((i=0; i < $fileCount - 1; i++))

do

file_array[$i]=$file

getFileSizeByte

getParamSizeByte

if [ $(echo "$fileSizeByte > $numParamByte"|bc) = 1 ]

then

cat /dev/null > $file_array

fi

done

done

fi

done

}

if test -d $1

then

scanAndDelFile $1

elif test -f $1

then

echo "you input a file but not a directory,pls reinput and try again"

exit 1

else

echo "the Directory isn't exist which you input,pls input a new one!!"

fi

实现2.根据文件名删除文件。定义正则表达式匹配文件中的字符串,运行脚本,匹配成功的文件被删除。

脚本实现代码如下:

#!/bin/bash

source conf.profile

echo infoFile=$infoFile

echo dirPathInConfig=$dirPathInConfig

echo strInConfig=$strInConfig

########################################################################################################

#############################根据指定字符串并匹配指定目录下的文件名删除文件#############################

function matchStrInfoAndDelFile(){

cat $infoFile | while read line

do

dirInfoInFile=`echo $line | awk '{print $1}'`

strInFile=`echo $line | awk '{print $2}'`

echo dirInfoInFile=$dirInfoInFile

echo strInFile=$strInFile

if [[ "$dirInfoInFile" != null ]]&&[[ ! -z "$strInFile" ]]

then

dirPath=$dirInfoInFile

str=$strInFile

elif [[ "$dirInfoInFile" == null ]]&&[[ ! -n $strInFile ]]

then

dirPath=$dirPathInConfig

str=$strInConfig

elif [[ "$dirInfoInFile" == null ]]&&[[ ! -z $strInFile ]]

then

dirPath=$dirPathInConfig

str=$strInFile

fi

echo dirPath=$dirPath

cd $dirPath

for file in `ls -l | awk '{print $9}'`

do

echo file=$file

matchStr=`echo $file | grep -o -E "$str"`

if [[ ! -z $matchStr ]]

then

echo 删除${file}文件

fi

done

# else

# echo 注释:$line

# fi

done

}

matchStrInfoAndDelFile

实现3.根据文件名中包含的日期信息删除文件,定义日期正则表达式,运行脚本,匹配成功的文件被删除。

脚本实现代码如下:

#!/bin/bash

source config.profile $infoFile

echo dirInfoInConfig=$dirInfoInConfig

echo dateRegexInConfig=$dateRegexInConfig

echo dateFormatInConfig=$dateFormatInConfig

echo daysInConfig=$daysInConfig

########################################################################################################

######################保留距当前日期days天的文件,之前的文件全部删除################################

function matchDateInfoAndDelFile(){

cat $infoFile | while read line

do

dirInfoInFile=`echo $line | awk '{print $1}'`

dateRegexInFile=`echo $line | awk '{print $2}'`

dateFormatInFile=`echo $line | awk '{print $3}'`

daysInFile=`echo $line | awk '{print $4}'`

echo dirInfoInFile=$dirInfoInFile

echo dateRegexInFile=$dateRegexInFile

echo dateFormatInFile=$dateFormatInFile

echo daysInFile=$daysInFile

if [[ "$dirInfoInFile" != null ]]&&[[ "$dateRegexInFile" != null ]]&&[[ "$dateFormatInFile" != null ]]&&[[ "$daysInFile" != null ]]

then

dirPath=$dirInfoInFile

dateRegex=$dateRegexInFile

dateFormat=$dateFormatInFile

days=$daysInFile

elif [[ "$dirInfoInFile" == null ]]&&[[ "$dateRegexInFile" != null ]]&&[[ "$dateFormatInFile" != null ]]&&[[ "$daysInFile" != null ]]

then

dirPath=$dirInfoInConfig

dateRegex=$dateRegexInFile

dateFormat=$dateFormatInFile

days=$daysInFile

elif [[ "$dirInfoInFile" == null ]]&&[[ "$dateRegexInFile" == null ]]&&[[ "$dateFormatInFile" != null ]]&&[[ "$daysInFile" != null ]]

then

dirPath=$dirInfoInConfig

dateRegex=$dateRegexInConfig

dateFormat=$dateFormatInFile

days=$daysInFile

elif [[ "$dirInfoInFile" == null ]]&&[[ "$dateRegexInFile" == null ]]&&[[ "$dateFormatInFile" == null ]]&&[[ "$daysInFile" != null ]]

then

dirPath=$dirInfoInConfig

dateRegex=$dateRegexInConfig

dateFormat=$dateFormatInConfig

days=$daysInFile

elif [[ "$dirInfoInFile" == null ]]&&[[ "$dateRegexInFile" == null ]]&&[[ "$dateFormatInFile" != null ]]&&[[ "$daysInFile" == null ]]

then

dirPath=$dirInfoInConfig

dateRegex=$dateRegexInConfig

dateFormat=$dateFormatInFile

days=$daysInConfig

elif [[ "$dirInfoInFile" != null ]]&&[[ "$dateRegexInFile" != null ]]&&[[ "$dateFormatInFile" != null ]]&&[[ "$daysInFile" == null ]]

then

dirPath=$dirInfoInFile

dateRegex=$dateRegexInFile

dateFormat=$dateFormatInFile

days=$daysInConfig

elif [[ "$dirInfoInFile" != null ]]&&[[ "$dateRegexInFile" != null ]]&&[[ "$dateFormatInFile" == null ]]&&[[ "$daysInFile" == null ]]

then

dirPath=$dirInfoInFile

dateRegex=$dateRegexInFile

dateFormat=$dateFormatInConfig

days=$daysInConfig

elif [[ "$dirInfoInFile" != null ]]&&[[ "$dateRegexInFile" == null ]]&&[[ "$dateFormatInFile" != null ]]&&[[ "$daysInFile" == null ]]

then

dirPath=$dirInfoInFile

dateRegex=$dateRegexInConfig

dateFormat=$dateFormatInFile

days=$daysInConfig

elif [[ "$dirInfoInFile" == null ]]&&[[ "$dateRegexInFile" != null ]]&&[[ "$dateFormatInFile" == null ]]&&[[ "$daysInFile" != null ]]

then

dirPath=$dirInfoInConfig

dateRegex=$dateRegexInFile

dateFormat=$dateFormatInConfig

days=$daysInFile

elif [[ "$dirInfoInFile" == null ]]&&[[ "$dateRegexInFile" != null ]]&&[[ "$dateFormatInFile" != null ]]&&[[ "$daysInFile" == null ]]

then

dirPath=$dirInfoInConfig

dateRegex=$dateRegexInFile

dateFormat=$dateFormatInFile

days=$daysInConfig

elif [[ "$dirInfoInFile" != null ]]&&[[ "$dateRegexInFile" == null ]]&&[[ "$dateFormatInFile" == null ]]&&[[ "$daysInFile" != null ]]

then

dirPath=$dirInfoInFile

dateRegex=$dateRegexInConfig

dateFormat=$dateFormatInConfig

days=$daysInFile

fi

echo dirInfo=$dirPath

dateBeforeNDays=`date -d "-${days}days" +"$dateFormat"`

cd $dirPath

for file in `ls -l|awk '{print $9}'`

do

echo file=$file

dateInFileName=`echo $file | grep -o -E "$dateRegex"`

echo dateInFileName=$dateInFileName

echo dateBeforeNDays=$dateBeforeNDays

if [[ "${dateInFileName}" < "${dateBeforeNDays}" ]]

then

echo 删除${file}文件

fi

done

done

}

matchDateInfoAndDelFile

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值