Shell脚本统计文件行数
awk '{print NR}' row_count.sh | tail -n1awk 'END{print NR}' row_count.shgrep -n "" row_count.sh|awk -F: '{print '} | tail -n1sed -n '$=' row_count.shwc -l row_count.shcat row_count.sh | wc -l
一、获取指定目录所有文件的行数:
#!/bin/bash filesCount=0
linesCount=0
function funCount()
{for file in ` ls $1 `doif [ -d $1"/"$file ];thenfunCount $1"/"$fileelsedeclare -i fileLinesfileLines=`sed -n '$=' $1"/"$file`let linesCount=$linesCount+$fileLineslet filesCount=$filesCount+1fidone
}if [ $# -gt 0 ];thenfor m_dir in $@dofunCount $m_dirdone
elsefunCount "."
fi
echo "filesCount = $filesCount"
echo "linesCount = $linesCount"
使用方法:
1、针对本目录
./count.sh
2、统计多个目录
./count.sh /tmp ~
二、获取指定目录特定扩展名文件的行数
#!/bin/bash
extens=(".c" ".cpp" ".h" ".hpp")
filesCount=0
linesCount=0
function funCount()
{for file in ` ls $1 `doif [ -d $1"/"$file ];thenfunCount $1"/"$fileelsefileName=$1"/"$fileEXTENSION="."${fileName##*.}echo "fileName = $fileName Extension = $EXTENSION"if [[ "${extens[@]/$EXTENSION/}" != "${extens[@]}" ]];thendeclare -i fileLinesfileLines=`sed -n '$=' $fileName`echo $fileName" : "$fileLineslet linesCount=$linesCount+$fileLineslet filesCount=$filesCount+1fifidone
}if [ $# -gt 0 ];thenfor m_dir in $@dofunCount $m_dirdone
elsefunCount "."
fi
echo "filesCount = $filesCount"
echo "linesCount = $linesCount"
使用方法:
1、针对本目录
./count.sh
2、统计多个目录
./count.sh /tmp ~