Linux provides many command line tools to simplify your daily tasks. One such tool is the wc command.
wc is your go-to command when you need to know the number of words in a file or how many files there are in a specific directory. But that's not all the wc command can do. Read on to learn what the wc command is and how to use it effectively on Linux.
The wc command prints out the number of lines, words, characters, or bytes in a file or output. Here's how to use it to your advantage.
The wc command is the abbreviation of word count. It is a command line tool that counts the number of words, lines, characters and bytes in the output. It comes pre-installed on every Unix and Linux-based operating system, so you don't need to install it manually.
To use wc, you need to specify a file or text output and the command options to use. The basic syntax of the wc command is:
wc [OPTION] [FILE]
There are many options available for use with the command, all of which we will discuss later. For command line help on the wc command, check its man page by running the following command:
man wc
“
For this example, create a file: linuxmi.txt. In this file, paste the following text:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it.[a] Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now.[b] If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea – let's do more of those!
“
This is the Zen of Python, a set of 19 guiding principles written by Tim Peters for writing simple, elegant, and concise Python code.
If you use the cat command to create the file, leave a blank line before pasting the text.
By default, when you use the wc command on a file or output, it prints out the number of lines, words, and bytes present in the output.
Try using linuxmi.txt by executing the following command in the terminal:
linuxmi@linuxmi:~/www.linuxmi.com$ wc linuxmi.txt
result:
19 137 830 linuxmi.txt
You will see that it outputs four columns containing the number of lines, the number of words, the number of bytes, and the file name.
To count the number of lines present in a file or output, use the -l or –lines option. The syntax is as follows:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -l linuxmi.txt
result:
19 linuxmi.txt
It shows that there are 19 lines in the file and also prints out the name of the text file.
To count the number of words in a file, use the -w or –words option. Try it:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -w linuxmi.txt
result:
137 linuxmi.txt
You can use the wc command with the -c or -****-bytes option to determine the exact number of bytes in the file. Execute the following command to try it out:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -c linuxmi.txt
result:
830 linuxmi.txt
要打印出文件中的字符数,请使用 -m 或 –chars 选项。语法如下所示:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -m linuxmi.txt
结果:
824 linuxmi.txt
如果需要知道文件中最长行的长度(该行中的字符数),请将 -L 或 –max 行长度选项与 wc 命令配合使用。它看起来像这样:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -L linuxmi.txt
结果:
70 linuxmi.txt
您可以将 wc 命令用于多个文件或输入。为此,您需要再创建两个文件。第一个文件是 zimu**.txt,其中包含字母表的列表,而第二个文件是shuzi.txt**,包含从 1 到 10 的数字列表。
或者,您可以使用任意两个文本文件。让我们来试试吧:
linuxmi@linuxmi:~/www.linuxmi.com$ wc linuxmi.txt zimu.txt shuzi.txt
结果如下图:
前三行包含每个文件的行数、字数和字节数,最后一行包含每列的总和。
您可以通过管道命令将 wc 与其他命令一起使用。管道符号将一个命令的输出作为输入重定向到另一个命令。
为此,您可以使用 ls 命令列出目录中的文件数,然后将输入通过管道传输到 wc 命令中。例如,要打印某一目录上的文件数,请执行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ ls www.linuxmi.com | wc -l
进程是您的计算机正在处理或当前正在运行的任务或程序。执行命令或打开应用程序时,该应用程序将注册为进程。
要计算进程数,请使用带有 wc 的 ps 命令。在这里,尝试一下:
linuxmi@linuxmi:~/www.linuxmi.com$ ps | wc -l
Linux 上有很多可用的命令,它们具有非常独特的功能,并使整体 Linux 体验无缝衔接。您只需要知道它们是什么以及如何使用它们!现在就开始你的 Linux 命令之旅吧!
The above is the detailed content of Simplify your daily tasks with Linux command line tools. For more information, please follow other related articles on the PHP Chinese website!