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
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteswc
Internet RequiredNo

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 

Output:

passing text file as an argument to wc command to get meta data

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 

Output:

Counting no of lines in text file using wc command

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 

Output:

Counting no of word in text file using wc command

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 

Output:

Counting no of bytes in text file using wc command

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 

Output:

Counting no of characters in text file using wc command

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 

Output:

Counting length of longest line in text file using wc command

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

Output:

Piping wc command with another ls command

The above command will consider each folder as a line and count each line to give you the total number of directories (ex: “10“) 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

Output:

Counting no of running processes running with wc and ps commands

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.

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

Output:

Counting specific word how much time repeat in text file using wc command

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.