shell 命令 wc 的使用简介

2021-08-26  本文已影响0人  Jamza

简介

wc 命令可以帮助我们计算文件的字节数、字数、或者文件的行数,常用于统计文件的相关信息。

wc 命令的 帮助信息比较简短:

[root@A23488811 test]# wc --help
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
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

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'wc invocation'

参数选项

支持的主要参数选项包括:

  1. -c:统计字节数
  2. -m:统计字符数,该参数不能与 -c 一起使用
  3. -l:统计行数
  4. -w:统计字数,这里的字指的是由空格、换行符等分隔的字符串

示例

使用以下的文件作为测试文件:

[root@A23488811 test]# cat testfile
Linux networks are becoming more and more common, but scurity is often an overlooked
issue. Unfortunately, in today’s environment all networks are potential hacker targets,
fro0m tp-secret military research networks to small home LANs.
Linux Network Securty focuses on securing Linux in a networked environment, where the
security of the entire network needs to be considered rather than just isolated machines.
It uses a mix of theory and practicl techniques to teach administrators how to install and
use security applications, as well as how the applcations work and why they are necesary.
[root@A23488811 test]#

对测试文件直接使用 wc 命令:

[root@A23488811 test]# wc testfile
  7  92 608 testfile

7 表示测试文件的行数,92 表示测试文件的字数,608 表示测试文件的字节数:

[root@A23488811 test]# wc -l testfile
7 testfile
[root@A23488811 test]# wc -w testfile
92 testfile
[root@A23488811 test]# wc -c testfile
608 testfile

可以通过命令输出的重定向功能,使得 wc 命令获得更广泛的使用方式,比如

[root@A23488811 test]# ll
total 16
drwxr-xr-x 2 root root   6 May 31 09:37 test
-rw-r--r-- 1 root root 608 May 31 09:29 testfile
-rw-r--r-- 1 root root 608 May 31 09:37 testfile1
-rw-r--r-- 1 root root 608 May 31 09:37 testfile2
-rw-r--r-- 1 root root 608 May 31 09:37 testfile3
[root@A23488811 test]#
[root@A23488811 test]#
[root@A23488811 test]# ll | grep "^-" | wc -l
4

以上命令可以统计当前目录下具有多少个普通文件。其中 grep "^-" 表示获取以 - 开头的行,这些行表示的都是普通文件,通过命令输出的重定向,作为 wc 命令的输入,可统计出当前目录下普通文件的数量。

上一篇 下一篇

猜你喜欢

热点阅读