shell统计指定目录、指定模式的文件行数

1、统计指定目录下文件个数(默认当前目录)

#!/bin/bash  
#统计指定目录下文件个数(默认当前目录)
count=0 
function funCount()
{
    for file in ` ls $1 `
    do
        if [ -f $1"/"$file ] 
	then  
		count=`expr $count + 1`
	fi  
    done
}
if [ $# -gt 0 ];then
    for m_dir in $@
    do
        funCount $m_dir
    done
else
    funCount "."
fi
echo "There are $count files."


实例:
[root@localhost sourcetemp]# sh filecount
There are 75 files . 


[root@localhost sourcetemp]# sh filecount  test
There are 75 files . 


2、根据配置文件读取指定的目录,再根据输入参数(模糊)匹配对应目录下的文件:

#!/bin/bash  
#统计指定目录下符合规则的文件个数 front_action.log.2015-05-14-*
count=0 
llines=0
function funCount()
{
    for file in ` ls $1 `
    do
		if [ $# -gt 1 ];then
			if [ -f $1"/"$file ]  
			then
				if [[ $1"/"$file = $1"/"$2 ]]
				then 
					declare -i fileLines  
					fileLines=`sed -n '$=' $1"/"$file`  
					let llines=$llines+$fileLines  
					count=`expr $count + 1`
				fi		
			fi  
		else
			if [ -f $1"/"$file ]  
			then
				declare -i fileLines  
				fileLines=`sed -n '$=' $1"/"$file`  
				let llines=$llines+$fileLines
				count=`expr $count + 1`	
			fi  		
		fi        
    done
}

dirs=`sed -n '/^[^#]/p' dirs`
if [ $# -eq 1 ];then
	for dir in $dirs  
    do
        funCount $dir $1
    done
elif [ $# -eq 0 ] ; then
    for dir in $dirs  
    do
        funCount $dir
    done
else 
	echo "args error!!!"
fi
echo "There are $count files."
echo "There are $llines lines."

实例:

[root@adiislogdata164 action]# sh c1.sh 
There are 410 files.
There are 398091119 lines.
[root@adiislogdata164 action]# sh c1.sh front_action.log.2015-05-13-*
There are 240 files.
There are 234195303 lines.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值