一天一個 Linux 命令(23):wc 命令


一、簡介

Linux系統里的wc(Word Count)是一個統計文件中的字節數、字數、行數等信息的命令,並將統計結果顯示輸出。

二、格式說明

wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F

wc [選項] [文件] ...

Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.  A word is a non-zero-length sequence of characters
delimited by white space.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

三、選項說明

-c 統計字節數

-m 統計字符數。這個標志不能與 -c 標志一起使用

-l 統計行數

-w 統計字數。一個字被定義為由空白、跳格或換行字符分隔的字符串

-L 打印最長行的長度

-help 顯示幫助信息

--version 顯示版本信息

四、命令功能

統計文件中的字節數、字數、行數,並將統計結果顯示輸出

五、常見用法

5.1 查看文件的字節數、字數、行數

#查看文件內容
# cat test.txt 
hello
i
love
China
,
my
name
is
joshua317 haha

#統計文件的行數、字數、字節數
# wc test.txt 
 9  10 47 test.txt 
 
#統計文件行數
# wc -l test.txt 
9 test.txt

#統計文件字節數
# wc -c test.txt 
47 test.txt

#統計字數
# wc -w test.txt 
10 test.txt

#統計字符數
# wc -m test.txt 
47 test.txt

#打印最長行的長度
# wc -L test.txt 
14 test.txt

5.2 使用管道符統計文件行數

# cat test.txt |wc -l
9

5.3 使用管道符統計當前目錄下的文件或者目錄數

ls -l|wc -l

注意:統計的數量中包含當前目錄

5.4 統計多個文件

# wc test.txt test2.txt 
 9 10 47 test.txt
 2  2  8 test2.txt
11 12 55 total

5.5 查看php的進程數量

# ps -ef|grep php|grep -v grep |wc -l
42

5.6 獲取當前目錄下所有符合條件的文件總數

# find ./ -type f | wc -l
1

5.7 統計目錄下atime時間大於365天的文件

# find . -atime +365 -exec ls -l {} \; | grep "^-" | wc -l
0

5.8 統計某文件夾下文件的個數

ls -l |grep "^-"|wc -l

5.9 統計某文件夾下目錄的個數

ls -l |grep "^d"|wc -l

注意:grep "^-"這里將長列表輸出信息過濾一部分,只保留一般文件,如果只保留目錄就是 ^d

5.10 統計文件夾下文件的個數,包括子文件夾里的

ls -lR|grep "^-"|wc -l

注意:ls -lR 長列表輸出該目錄下文件信息(R代表子目錄注意這里的文件,不同於一般的文件,可能是目錄、鏈接、設備文件等)

5.11 統計目錄(包含子目錄)下的所有帶有js字符的文件或者目錄

ls -lR|grep js|wc -l

持續整理。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM