Linux comes with many pre-installed tools. The wc command is one of them. The term “wc” is an abbreviation of “word count”. As the name suggests, the wc command is for counting various values of a file. These counts can come in handy in various situations.

In this guide, check out using the wc command with examples.

Linux wc command

The wc command is a tool that comes pre-installed in any Linux distro. It’s a tool dedicated to counting various things, for example, words, lines, characters, and bytes. As for the input, it can be STDIN (standard input) or a file.

The man page of wc explains all the available options in detail.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-01.png" data-lazy- height="1460" src="data:image/svg xml,” width=”2889″>

Using wc command

Wc command structure

This is the base structure of the wc command.

As for the input, wc accepts zero or more input “FILE” names. If no name is specified, then wc will operate on STDIN.

Basic usage

If no option is specified, then wc will print all the info about the input file: line, word, and character count. If the input is multiple files, then wc will print info about each of them separately.

For demonstration, we’ll use the GPL 3 license description. Check out GPL 3.

$ wget https://www.gnu.org/licenses/gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-02.png" data-lazy- height="614" src="data:image/svg xml,” width=”2893″>

Now, run wc on the text file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-03.png" data-lazy- height="208" src="data:image/svg xml,” width=”2889″>

The output is divided into 4 columns. Note that wc will always print the output in the following order.

  • Column 1: Line count.
  • Column 2: Word count.
  • Column 3: Character count.
  • Column 4: Filename and file path.

We can pipe the command’s output to wc to count words, lines, characters, and bytes. In such a situation, however, column 4 will be empty.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-04.png" data-lazy- height="212" src="data:image/svg xml,” width=”2889″>

Let’s see what happens when there are multiple files as input.

$ wc dummy.txt gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-05.png" data-lazy- height="287" src="data:image/svg xml,” width=”2885″>

Line count

Although the wc command can print various info all at once, it’s unnecessary in most cases. We can individually check various file info.

To check the number of lines in the file, use the flag “-l” or “–lines”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-06.png" data-lazy- height="204" src="data:image/svg xml,” width=”2893″>

Here’s how to check the line count of multiple files.

$ wc –lines dummy.txt gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-07.png" data-lazy- height="287" src="data:image/svg xml,” width=”2889″>

Word count

Although we all have an innate understanding of what a word is, it’s necessary to understand the technical definition to make sense of wc output. In the case of wc, a word doesn’t have to be part of the dictionary. Instead, it’s defined as a string of characters delimited using space, tab, or newline.

To count the word of a file, use the flag “-w” or “–words”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-08.png" data-lazy- height="224" src="data:image/svg xml,” width=”2891″>

Character count

Use the flag “-m” or “–chars” to get the character count of the input.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-11.png" data-lazy- height="210" src="data:image/svg xml,” width=”2889″>

Byte count

The wc command can also tell the size of a file in bytes. A byte is a unit comprising of 8 bits.

To get a byte count of the target file/input, use the flag “-c” or “–bytes”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-12.png" data-lazy- height="212" src="data:image/svg xml,” width=”2889″>

Maximum line length

In the case of wc, a line can be any collection of characters (a string) delimited by a new line. If there are multiple lines, then certain lines may contain more characters than any other. We can use wc to get the length of the longest line in the input.

To do so, use the flag “–max-line-length” or “-L”.

$ wc –max-line-length gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-13.png" data-lazy- height="214" src="data:image/svg xml,” width=”2889″>

Practical examples

So far, we’ve checked using the wc command only. However, we can combine wc with other commands to get more valuable information. Here are a handful of examples.

Counting number of files

We can use wc to count the number of files in a directory.

The trick is using the find command and wc command in combination. The find command will print the list of files in the directory, and wc will count the lines. Check out this in-depth guide on Linux find command.

$ find -type f | wc -l

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-14.png" data-lazy- height="579" src="data:image/svg xml,” width=”2889″>

Counting user numbers

In Linux, the file “/etc/passwd” contains all the users and user info that use passwords as the authentication mechanism. By counting the number of lines in the file, we can check the number of users currently in the system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-15.png" data-lazy- height="202" src="data:image/svg xml,” width=”2889″>

If users are configured to use LDAP as an authentication method, those users won’t appear in this file. To get the list of all the users, the getent command is a good option.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/wc-Command-Linux-16.png" data-lazy- height="204" src="data:image/svg xml,” width=”2889″>

The getent tool is dedicated to displaying info stored in various administrative databases. Here, “passwd” is the database that holds all the user info. Learn more about Linux getent command.

Final thoughts

The wc command is a simple and easy-to-use tool that comes in handy in various situations. It gives a quick count of lines, words, bytes, and characters of a file.

However, a file has more properties and parameters than that. The ls command can give you in-depth info about file permissions, location, size, ownership, and more. Check out how to use Linux ls command.

Happy computing!

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/profile-photo-1-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.