为 Linux 初学者讲解 wc 命令
wc
![](/data/attachment/album/201802/09/232919mj5ahh8apsuepydt.jpg)


在命令行工作时，有时您可能想要知道一个文件中的单词数量、字节数、甚至换行数量。如果您正在寻找这样做的工具，您会很高兴地知道，在 Linux 中，存在一个命令行实用程序，它被称为 `wc` ，它为您完成所有这些工作。在本文中，我们将通过简单易懂的例子来讨论这个工具。


但是在我们开始之前，值得一提的是，本教程中提供的所有示例都在 Ubuntu 16.04 上进行了测试。


### Linux wc 命令


`wc` 命令打印每个输入文件的新行、单词和字节数。以下是该命令行工具的语法:



```
wc [OPTION]... [FILE]...

```

以下是 `wc` 的 man 文档的解释：



```
为每个文件打印新行、单词和字节数，如果指定多于一个文件，也列出总的行数。单词是由空格分隔的非零长度的字符序列。如果没有指定文件，或当文件为 `-`，则读取标准输入。

```

下面的 Q&A 样式的示例将会让您更好地了解 `wc` 命令的基本用法。


注意：在所有示例中我们将使用一个名为 `file.txt` 的文件作为输入文件。以下是该文件包含的内容：



```
hi
hello
how are you
thanks.

```

### Q1. 如何打印字节数


使用 `-c` 命令选项打印字节数.



```
wc -c file.txt

```

下面是这个命令在我们的系统上产生的输出：


[![如何打印字节数](/data/attachment/album/201802/09/232932ah1iqtgkcn1knddn.png)](https://www.howtoforge.com/images/usage_of_pfsense_to_block_dos_attack_/big/wc-c-option.png)


文件包含 29 个字节。


### Q2. 如何打印字符数


要打印字符数，请使用 `-m` 命令行选项。



```
wc -m file.txt

```

下面是这个命令在我们的系统上产生的输出：


[![如何打印字符数](/data/attachment/album/201802/09/232934j44lgkertkl1vtsg.png)](https://www.howtoforge.com/images/usage_of_pfsense_to_block_dos_attack_/big/wc-m-option.png)


文件包含 29 个字符。


### Q3. 如何打印换行数


使用 `-l` 命令选项来打印文件中的新行数：



```
wc -l file.txt

```

这里是我们的例子的输出：


[![如何打印换行数](/data/attachment/album/201802/09/232936ubbehhsephhmvv7v.png)](https://www.howtoforge.com/images/usage_of_pfsense_to_block_dos_attack_/big/wc-l-option.png)


### Q4. 如何打印单词数


要打印文件中的单词数量，请使用 `-w` 命令选项。



```
wc -w file.txt

```

在我们的例子中命令的输出如下：


[![如何打印字数](/data/attachment/album/201802/09/232938es50762fhnb5sa01.png)](https://www.howtoforge.com/images/usage_of_pfsense_to_block_dos_attack_/big/wc-w-option.png)


这显示文件中有 6 个单词。


### Q5. 如何打印最长行的显示宽度或长度


如果您想要打印输入文件中最长行的长度，请使用 `-l` 命令行选项。



```
wc -L file.txt

```

下面是在我们的案例中命令产生的结果：


[![如何打印最长行的显示宽度或长度](/data/attachment/album/201802/09/232940ovfdjojdsvufwut8.png)](https://www.howtoforge.com/images/usage_of_pfsense_to_block_dos_attack_/big/wc-L-option.png)


所以文件中最长的行长度是 11。


### Q6. 如何从文件读取输入文件名


如果您有多个文件名，并且您希望 `wc` 从一个文件中读取它们，那么使用`-files0-from` 选项。



```
wc --files0-from=names.txt

```

[![如何从文件读取输入文件名](/data/attachment/album/201802/09/232941aux4u1qphz4epmjg.png)](https://www.howtoforge.com/images/usage_of_pfsense_to_block_dos_attack_/big/wc-file0-from-option.png)


如你所见 `wc` 命令，在这个例子中，输出了文件 `file.txt` 的行、单词和字符计数。文件名为 `file.txt` 的文件在 `name.txt` 文件中提及。值得一提的是，要成功地使用这个选项，文件中的文件名应该用 NUL 终止——您可以通过键入`Ctrl + v` 然后按 `Ctrl + Shift + @` 来生成这个字符。


### 结论


正如您所认同的一样，从理解和使用目的来看， `wc` 是一个简单的命令。我们已经介绍了几乎所有的命令行选项，所以一旦你练习了我们这里介绍的内容，您就可以随时在日常工作中使用该工具了。想了解更多关于 `wc` 的信息，请参考它的 [man 文档](https://linux.die.net/man/1/wc)。




---


via: <https://www.howtoforge.com/linux-wc-command-explained-for-beginners-6-examples/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[stevenzdg988](https://github.com/stevenzdg988) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux wc Command Explained for Beginners (6 Examples)

While working on the command line, sometimes you may want to access the number of words, byte counts, or even newlines in a file. If you are looking for a tool to do this, you'll be glad to know that in Linux, there exists a command line utility - dubbed **wc** - that does all this for you. In this article, we will be discussing this tool through easy to understand examples.

But before we jump in, it's worth mentioning that all examples provided in this tutorial have been tested on Ubuntu 16.04.

## Linux wc command

The wc command prints newline, word, and byte counts for each input file. Following is the syntax of this command line tool:

wc [OPTION]... [FILE]...

And here's how wc's man page explains it:

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. With no

FILE, or when FILE is -, read standard input.

The following Q&A-styled examples will give you an even better idea about the basic usage of wc.

Note: We'll be using a file named file.txt as the input file in all our examples. Following is what the file contains:

hi

hello

how are you

thanks.

## Q1. How to print the byte count

Use the **-c** command line option to print the byte count.

wc -c file.txt

Here's the output this command produced on our system:

So the file contains 29 bytes.

## Q2. How to print the character count

To print the number of characters, use the **-m** command line option.

wc -m file.txt

Here's the output this command produced on our system:

So the file contains 29 characters.

## Q3. How to print the newline count

Use the **-l** command line option to print the number of newlines in the file.

wc -l file.txt

Here's the output in our case:

## Q4. How to print the word count

To print the number of words present in the file, use the **-w** command line option.

wc -w file.txt

Following the output the command produced in our case:

So this reveals there are 6 words in the file.

## Q5. How to print the maximum display width or length of longest line

In case you want to print the length of the longest line in the input file, use the **-L** command line option.

wc -L file.txt

Here's the output the command produced in our case:

So the length of the longest file in our file is 11.

## Q6. How to read input file name(s) from a file

In case you have multiple file names, and you want wc to read them from a file, then use the **--files0-from** option.

wc --files0-from=names.txt

So you can see that the wc command, in this case, produced lines, words, and characters count for file.txt in the output. The name file.txt was mentioned in the names.txt file. It's worth mentioning that to successfully use this option, names written the file should be NUL terminated - you can generate this character by typing `Ctrl`+`v` followed by `Ctrl`+`Shift`+`@.`

## Conclusion

As you'd agree, wc is a simple command, both from understanding and usage purposes. We've covered pretty much all command line options the tool offers, so you should be ready to use the tool on a daily basis once you practice whatever we've explained here. For more info on wc, head to its [man page](https://linux.die.net/man/1/wc).