Linux下统计相关的命令

2019-06-27  本文已影响0人  Jerry_1116

作为编程人员,经常可能有统计代码行数、指定格式的文件数量、单词数量等需求。再Linux操作系统下,有很多命令/工具可以快速实现。

1 wc命令

1.1 wc命令的功能

wc命令的功能主要有:

1.2 wc命令的用法

$ wc --help
用法:wc [选项]... [文件]...
 或:wc [选项]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  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 maximum display width
  -w, --words            print the word counts
      --help        显示此帮助信息并退出
      --version     显示版本信息并退出

1.3 wc应用示例

  1. 统计当前目录(及其子目录)下的所有.py文件数量
$ find . -name "*.py" | wc -l
39
  1. 统计当前目录(及其子目录)下的所有文件行数

(1)只显示所有文件的行数:

$ find . -name "*.*" |xargs cat|wc -l
cat: .: 是一个目录
214

(2)显示每个文件的行数,以及总的行数

$ wc -l `find . -name "*.*"`
wc: .: 是一个目录
      0 .
     99 ./redux/timewindow.spec.ts
    115 ./redux/localsettings.ts
 214 总用量
  1. 统计当前目录(及其子目录)下所有文件行数,并过滤空行
$ find . -name "*.*" |xargs cat|grep -v ^$|wc -l
cat: .: 是一个目录
180
  1. 统计当前目录下的文件数
$ ls -l | wc -l
2
上一篇 下一篇

猜你喜欢

热点阅读