??因程序執行過程中生成大量的日志,導致日志文件迅速增大,占用大量磁盤空間,影響到程序正常運行,程序執行速度減慢
??在指定文件中保存目錄路徑,并指定刪除條件(如:大小值,特定字符串正則表達式、日期正則表達式),運行腳本讀取該文件,若符合條件,則刪除該文件。
? 實現: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