Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux wc 命令詳解:輕鬆統計檔案的行數、字數與位元組

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

說明:統計數量中包含「總計」那一行,因此實際檔案數需再減一。

Copyright © Linux教程網 All Rights Reserved