获取单个文件行数
行数:@H_404_9@20
方法一
@H_404_9@awk '{print NR}' test1.sh|tail -n1
如图所示:
方法二
@H_404_9@awk 'END{print NR}' test1.sh
如图所示:
方法三
@H_404_9@grep -n "" test1.sh|awk -F: '{print '}|tail -n1
如图所示:
方法四
@H_404_9@sed -n '$=' test1.sh
如图所示:
方法五
@H_404_9@wc -l test1.sh
如图所示:
方法六
@H_404_9@#cat test1.sh |wc -l
如图所示:
获取特定目录所有文件的行数
代码如下:
#!/bin/bash filesCount=0 linesCount=0 function funCount() { for file in ` ls $1 ` do if [ -d $1"/"$file ];then funCount $1"/"$file else declare -i fileLines fileLines=`sed -n '$=' $1"/"$file` let linesCount=$linesCount+$fileLines let filesCount=$filesCount+1 fi done } if [ $# -gt 0 ];then for m_dir in $@ do funCount $m_dir done else funCount "." fi echo "filesCount = $filesCount" echo "linesCount = $linesCount"
使用方法:
@H_404_9@1、针对本目录
@H_404_9@./count.sh
@H_404_9@./count.sh /tmp ~
运行效果如下:
获取特定目录特定扩展名文件的行数
代码如下:
#!/bin/bash extens=(".c" ".cpp" ".h" ".hpp") filesCount=0 linesCount=0 function funCount() { for file in ` ls $1 ` do if [ -d $1"/"$file ];then funCount $1"/"$file else fileName=$1"/"$file EXTENSION="."${fileName##*.} echo "fileName = $fileName Extension = $EXTENSION" if [[ "${extens[@]/$EXTENSION/}" != "${extens[@]}" ]];then declare -i fileLines fileLines=`sed -n '$=' $fileName` echo $fileName" : "$fileLines let linesCount=$linesCount+$fileLines let filesCount=$filesCount+1 fi fi done } if [ $# -gt 0 ];then for m_dir in $@ do funCount $m_dir done else funCount "." fi echo "filesCount = $filesCount" echo "linesCount = $linesCount"
使用方法:
@H_404_9@./count.sh /tmp ~
运行效果如下:
好,就这些了,希望对你有帮助。
原文链接:https://www.f2er.com/bash/390886.html