How to Use wc Command in Linux

5

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.

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

How to Use wc Command in Linux How to Use wc Command in Linux 1625345538 943 How to Use wc Command in Linux

Now, run wc on the text file.

How to Use wc Command in Linux How to Use wc Command in Linux 1625345539 401 How to Use wc Command in Linux

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.

How to Use wc Command in Linux How to Use wc Command in Linux 1625345540 668 How to Use wc Command in Linux

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

$ wc dummy.txt gpl-3.0.txt

How to Use wc Command in Linux How to Use wc Command in Linux 1625345540 996 How to Use wc Command in Linux

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”.

How to Use wc Command in Linux How to Use wc Command in Linux 1625345541 355 How to Use wc Command in Linux

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

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

How to Use wc Command in Linux How to Use wc Command in Linux 1625345542 735 How to Use wc Command in Linux

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”.

How to Use wc Command in Linux How to Use wc Command in Linux 1625345543 76 How to Use wc Command in Linux

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

How to Use wc Command in Linux How to Use wc Command in Linux 1625345543 568 How to Use wc Command in Linux

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”.

How to Use wc Command in Linux How to Use wc Command in Linux 1625345544 580 How to Use wc Command in Linux

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

How to Use wc Command in Linux How to Use wc Command in Linux 1625345544 632 How to Use wc Command in Linux

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

How to Use wc Command in Linux How to Use wc Command in Linux 1625345545 859 How to Use wc Command in Linux

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.

How to Use wc Command in Linux How to Use wc Command in Linux 1625345546 683 How to Use wc Command in Linux

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.

How to Use wc Command in Linux How to Use wc Command in Linux 1625345546 925 How to Use wc Command in Linux

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!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More