Beginners Guide for WC Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The WC command is used to count various parameters from the specified file, like the number of lines, word counts, byte counts, character counts, and many more.

Tutorial Details

DescriptionWC (Word Count)
Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisiteswc
Internet RequiredNo
Discussed Tools in this Article

Syntax of the WC Command

The WC command takes two arguments: one is the flag, and the other is the file path.

$ wc [OPTION] [FILE]

Creating a Test File for the WC Command

Create a new text file with the name “file.txt” and copy the following content into it, then save the file in your home directory.

this is line one
this is line two
this is line three
this is line four
this is line five

We will use this text file to learn the WC command and its various flags.

Printing Newline, Word, and Byte Counts for a Specified File

The WC command without any flags will print the various parameters of the specified file.

$ wc file.txt 
 5 20 89 file.txt

Output breakdown:

Newline countsWord CountsByte CountsFile Name
52089file.txt

However, you can print the specific parameters using the WC flags.

Counting All Lines in a File

The “-l” or “--lines” flag will count all the lines in the specified file.

$ wc -l file.txt 
5 file.txt

Counting All Words in a File

Use the “-w” or “--words” flag to count all the words in the specified file.

$ wc -w file.txt 
20 file.txt

Counting All Bytes in a File

The “-c” or “--bytes” flag will print the number of bytes in the specified file.

$ wc -c file.txt 
89 file.txt

Counting All Characters in a File

The “-w” or “--chars” flag will print the total number of characters in the specified file.

$ wc -m file.txt 
89 file.txt

Since one character (except a multi byte character like an emoji) is counted as one byte, the output for this command will be identical to the byte count.

Counting the Length of the Longest Line in Number of Characters

The “-L” or “--max-line-length” flag will only count the length of the longest line in the specified file and print the number of characters.

$ wc -L file.txt 
18 file.txt

Piping the Other Commands Output to the WC Command

You can redirect the commands’ (Stdout) output to the WC command (Stdin) to count the number of lines, words, bytes, and characters.

For example, to count the number of folders in your home directory, execute the following command:

$ ls | wc -l
11

The above command will consider each folder as a line and count each line to give you the total number of directories (ex: “11“) in your home directory.

Next, you can count the total number of active processes for the current user using the PS and WC commands.

$ ps --no-header | wc -l
4

Note that the above command also includes the process for the PS and WC commands, so you have to ignore them to get the actual running process (2) besides them.

Also Read: How to Count String Occurrences in a Text File

Counting the Number of Word Occurrences in the File

To count the number of word occurrences in the specified file, you can use the grep command with the word you want to find and the “-i” flag to find the number of times the word appeared in the text file, and then use the “-o” flag to print each match in a unique line, and lastly redirect the output to the WC command.

$ grep -oi this file.txt | wc -l
6

And that was the last example.

If you have a question or suggestion about this article, feel free to tell us in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.