统计指定文件个数

介绍

背景:统计指定目录下出现关键词的文件有多少个,需要通过另一个文件关键词来确定需要统计的的文件,一个文件夹下面有多个文件租成一个文件,需要统计指定时间内的文件

脚本

 #!/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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值