This article will introduce wc
command. You can use wc
command to count the number of lines, words and bytes in the file.
Wc command syntax
Table of Contents
There are 2 ways to use the wc
command as below.
We use wc
to count the number of lines in a file, not the data in the pipeline.
$ wc [options] filename
Use wc
in the pipeline, processing the output data from another command.
$ command-1 | command-2 | wc [options]
Use wc command to count number lines
Okey, now we will do an example. The first example is to use the non-pipeline command.
We have a text file with the following content. Now we will count the lines, count the words, count the number of bytes of that text file.
$ wc demo-wc.txt
For the second example, we will reuse the example included in the article about sort command. We will list the content in /bin
and /usr/bin
and sort it. Now we will count the number files in both folders after sorting.
$ ls /bin /usr/bin | sort | wc -l
2748
- Use
-c
to print the byte counts. - Use
-l
to print the newline counts. - And use
-w
to print the word counts.
Conclusion
The wc command is often used in conjunction with the sort and uniq command. This set of 3 commands creates very powerful data filtering pipelines. You should combine it.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).