一天一个 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

持续整理。。。

 

原文地址:https://www.cnblogs.com/joshua317/p/15388242.html