分享一個統計代碼行的shell命令:
find . “(” -name “.java” -or -name “.html” -or -name “.js” -or -name “.css” “)” -print | xargs wc -l
根據不同的項目類型,不同的目錄,自行修改。
如果內容多,可以:
find . “(” -name “.java” -or -name “.html” -or -name “.js” -or -name “.css” “)” -print | xargs wc -l > statistics.txt
補充:下面看下統計行數、字符數、字節數的shell腳本
在linux中,有一個常用的命令wc,可用來統計文件的行數、單詞數、字符數以及字節數。
運行wc命令時,必須明確指定命令中的子文件夾的層級。
通常情況下,用于統計的文件夾,會有很多的層級,會使統計變得復雜。
本文提供的這個腳本,可以幫助我們搜索各級子文件夾,并給出每個文件夾級別的計數,并將結果存儲在一個單獨的文件,以供將來使用。
代碼:
代碼示例:
#!/bin/bash
#filename: count_lines.sh
len=$(echo $1 |awk '{print length($0)}')
len2=$(echo $2 |awk '{print length($0)}')
if [ $len -ne "0" ]
then
x="$1"
i="1"
cc=0
while [ $i -le "$len" ]
do
echo `expr substr $1 $i 1 `
if [ `expr substr $1 $i 1 ` = "l" ] || [ `expr substr $1 $i 1 ` = "w" ] || [ `expr substr $1 $i 1 ` = "c" ] || [ `expr substr $1 $i 1 ` = "k" ]
then
lengh = "length=$(echo "$1" |awk '{print length($0)}')"
else
cc=`expr $cc + 1`
fi
i=`expr $i + 1`
done
if [ $cc -eq "0" ]
then
rm ~/count_output
echo "=======================current Directory==========================" >> count_output
pwd >> count_output
if [ $len2 -eq "0" ]
then
ls -l * | wc -$1 *.* | tail -k >> ~/count_output
else
ls -l * | wc -$1 *.$2 | tail -k >> ~/count_output
fi
ls -R | grep './' > list
sed "s/:/ /" list >> list1
cat list1 | while read current_dir
do
echo "=======================$current_dir ==========================" >> ~/count_output
cd $current_dir
if [ $len2 -eq "0" ]
then
ls -l * | wc -$1 *.* | tail -k >> ~/count_output
else
ls -l * | wc -$1 *.$2 | tail -k >> ~/count_output
fi
cd -
done
clear
rm list
rm list1
else
print "invalid entry"
fi
else
print "input options l w c k needs to be entered"
fi
總結
以上所述是小編給大家介紹的shell 命令統計代碼行數的簡單代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
您可能感興趣的文章:- Shell腳本實現C語言代碼行數統計
- shell判斷文件是否為空的代碼
- shell腳本 自動創建用戶詳解及實例代碼
- shell腳本實現同時多臺遠程主機執行命令的代碼分享
- PowerShell連接SQL SERVER數據庫進行操作的實現代碼
- Powershell 之批量獲取文件大小的實現代碼