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.
|Description||WC (Word Count)|
|Root or Sudo Privileges||No|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
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
|Newline counts||Word Counts||Byte Counts||File Name|
However, you can print the specific parameters using the WC flags.
Counting All Lines in a File
-l” or “
--lines” flag will count all the lines in the specified file.
$ wc -l 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
Counting All Bytes in a File
-c” or “
--bytes” flag will print the number of bytes in the specified file.
$ wc -c file.txt
Counting All Characters in a File
-w” or “
--chars” flag will print the total number of characters in the specified file.
$ wc -m 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
-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
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
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
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
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.