b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

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

电脑杂谈  发布时间:2020-06-08 06:16:19  来源:网络整理

shell脚本删除文件_shell脚本学习指南 下载_shell调用shell脚本

由于程序执行过程中产生大量日志,日志文件迅速增加,占用大量磁盘空间,影响程序的正常运行,程序的执行速度变慢<

将目录路径保存在指定文件中,并指定删除条件(例如: 大小值,特定字符串正则表达式,日期正则表达式),运行脚本以读取文件shell脚本删除文件,如果满足条件,则删除文件.

shell脚本学习指南 下载_shell调用shell脚本_shell脚本删除文件

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

脚本实现代码如下:

shell调用shell脚本_shell脚本学习指南 下载_shell脚本删除文件

#!/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.根据文件名删除文件. 定义一个正则表达式以匹配文件中的字符串,运行脚本,然后删除成功匹配的文件.

shell脚本学习指南 下载_shell脚本删除文件_shell调用shell脚本

脚本实现代码如下:

#!/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

shell脚本删除文件_shell调用shell脚本_shell脚本学习指南 下载

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

脚本实现代码如下:


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/sanxing/article-238001-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...