每天一個linux命令(ling)(40):wc命令(ling)
Linux系統中的wc(Word Count)命令的功能為統計(ji)指定文(wen)件中的字節數、字數、行(xing)數,并將統計(ji)結果顯示輸出。
1.命(ming)令格式:
wc [選項]文件...
2.命(ming)令功(gong)能:
統(tong)(tong)計(ji)(ji)指(zhi)定文(wen)件中的字(zi)(zi)節數(shu)(shu)、字(zi)(zi)數(shu)(shu)、行數(shu)(shu),并將統(tong)(tong)計(ji)(ji)結(jie)果顯(xian)示輸(shu)出(chu)。該(gai)命令統(tong)(tong)計(ji)(ji)指(zhi)定文(wen)件中的字(zi)(zi)節數(shu)(shu)、字(zi)(zi)數(shu)(shu)、行數(shu)(shu)。如果沒有給出(chu)文(wen)件名,則從標準輸(shu)入(ru)讀取。wc同時也給出(chu)所指(zhi)定文(wen)件的總統(tong)(tong)計(ji)(ji)數(shu)(shu)。
3.命令參數:
-c 統(tong)計字節數。
-l 統計行數(shu)。
-m 統計字(zi)符數。這個標志不(bu)能與 -c 標志一起使用。
-w 統計字數。一個字被定義為(wei)由(you)空白、跳格或換行字符分(fen)隔的字符串。
-L 打印最長行的長度。
-help 顯(xian)示幫(bang)助信息
--version 顯示版(ban)本信(xin)息(xi)
4.使用(yong)實例:
實例1:查看文件的字節數、字數、行數
命令(ling):
wc test.txt
輸出(chu):
[root@localhost test]# cat test.txt
hnlinux
peida.ywjunkang.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[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 -m test.txt
70 test.txt
[root@localhost test]# wc -L test.txt
17 test.txt
說明:
7 8 70 test.txt
行數 單詞數 字節數 文件名
實(shi)例(li)2:用wc命令(ling)怎么做到只(zhi)打印統(tong)計數字不打印文件名
命令:
輸出:
[root@localhost test]# wc -l test.txt
7 test.txt
[root@localhost test]# cat test.txt |wc -l
7[root@localhost test]#
說明(ming):
使用管(guan)道線,這(zhe)在(zai)編寫(xie)shell腳本時特別有用。
實例3:用來統計當前目錄下的文件(jian)數
命令:
ls -l | wc -l
輸(shu)出:
[root@localhost test]# cd test6
[root@localhost test6]# ll
總計 604
---xr--r-- 1 root mail 302108 11-30 08:39 linklog.log
---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 mail users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2017.log
[root@localhost test6]# ls -l | wc -l
8
[root@localhost test6]#
說明(ming):
數量中包含當前目(mu)錄
關注 熵減黑客 ,一起學習成長
