介绍
背景:统计指定目录下出现关键词的文件有多少个,需要通过另一个文件关键词来确定需要统计的的文件,一个文件夹下面有多个文件租成一个文件,需要统计指定时间内的文件
脚本
#!/bin/bash
function getdir() {
# 获取文件列表
filelist=`ls $1`
export isKey=false
export isfileKey=false
for element in $filelist
do
dir_or_file=$1"/"$element
if [ -d $dir_or_file ]
then
# 如果是文件夹 就进行
getdir $dir_or_file
else
if [ "${dir_or_file#*.}"x = "jpg.ocr.json"x ] || [ "${dir_or_file#*.}"x = "png.ocr.json"x ]
then
# 只对指定的文件后缀进行过滤
a=`stat -c %Y $dir_or_file`
b=`date +%s`
if [ $[ $b - $a ] -lt 345600 ]
then
# 查询四天内的数据
# 检查文件内容是否含义指定的key
check_results=`grep $key $dir_or_file |wc -l `
if [ $check_results -eq 1 ]
then
#echo $dir_or_file
export isKey=true
fi
# 检查文件内容是否指定的文件
check_results=`grep $fileKey $dir_or_file |wc -l `
if [ $check_results -eq 1 ]
then
export isfileKey = true
fi
fi
fi
fi
done
# 统计文件是否符合查询条件
if [ $isKey == true ]
then
export keySum=$(($keySum + 1))
fi
if [ $isfileKey == true ]
then
export fileKeySum=$(($fileKeySum + 1))
fi
if [ $isKey == true ] && [ $isfileKey == false ]
then
# 虽然含有查询关键词 但没有包含 文件关键词
echo $1
fi
}
root_dir="/data/file/"
#sum
export keySum=0
export key=$1
export fileKeySum=0
export fileKey=$2
getdir $root_dir
echo $key+"出现了" +$keySum + ”次“
echo $fileKey + "出现了"+ $fileKeySum +"次"
使用
./stat.sh key keyFile