Linux 中的 wc(Word Count)指令,主要用來統計指定檔案的位元組數、字數與行數,並將結果輸出顯示。
wc [選項] 檔案...
統計指定檔案的位元組數、字數、行數並輸出結果。若未提供檔名,則從標準輸入讀取;同時也會給出所有指定檔案的總統計數。
-c 統計位元組數
-l 統計行數
-m 統計字元數(不能與 -c 併用)
-w 統計字數(以空白、跳格或換行分隔)
-L 印出最長行的長度
--help 說明 --version 版本資訊
指令:wc test.txt
輸出:
[root@localhost test]# wc test.txt
7 8 70 test.txt
[root@localhost test]# wc -l test.txt
7 test.txt
[root@localhost test]# wc -c test.txt
70 test.txt
[root@localhost test]# wc -w test.txt
8 test.txt
[root@localhost test]# wc -L test.txt
17 test.txt
說明:輸出依序為 行數、單詞數、位元組數、檔名。
指令:cat test.txt | wc -l
輸出:
[root@localhost test]# cat test.txt |wc -l
7
說明:透過管道線即可只輸出數字,這在撰寫 shell 腳本時特別實用。
指令:ls -l | wc -l
輸出:
[root@localhost test6]# ls -l | wc -l
8
說明:統計數量中包含「總計」那一行,因此實際檔案數需再減一。