Linux Commands: wc

UY

Umesh Yadav / October 11th, 2020

2 min read343 words

wc stands for word count. It is used to display the number of lines, words, characters, and byte for a file or input received via pipe(|).

Screenshot 2020-10-11 at 10.50.59 PM.png

the output shown above is explained below.

  • 1 - Number of lines.
  • 2 - Number of words
  • 14 - Number of bytes

There are four options that can be used with wc.

  • -c: The number of bytes in each input file is written to the standard output.
  • -l: The number of lines in each input file is written to the standard output.
  • -m: The number of characters in each input file is written to the standard output. If the current locale does not support multi-byte characters, this is equivalent to the -c option.
  • -w: The number of words in each input file is written to the standard output.

Let's see these options in action.

This will count the lines.

bash

$ wc -l hashnode.txt
1 hashnode.txt

This will count the words.

bash

$ wc -w hashnode.txt
2 hashnode.txt

This will count the number of bytes.

bash

$ wc -c hashnode.txt
14 hashnode.txt

But sometimes you have a file with multi-byte(non-ASCII charsets). For example, the Unicode character where one character is represented by multiple bytes.

Screenshot 2020-10-11 at 11.04.09 PM.png

If you see this carefully, we added an emoji character to the end of the file. Both -c and -m output differ from each other because of that.

You can also pass more than one file to the wc as input.

Screenshot 2020-10-11 at 11.08.56 PM.png

Advance Usage.#

You can also apply the wc on the output of other commands. Screenshot 2020-10-11 at 11.11.41 PM.png

bash

$ ls -al | wc
118 1065 7139

Conclusion#

wc is very helpful and can be used quite frequently. Let me know in comment how you use it.

If you liked this post please share it with others so that it can help them as well. You can tag me on twitter @imumesh18.

Umesh's Newsletter

Subscribe to get my latest blogs delivered straight into your inbox