Linux wc command is used to count the number of words, lines, and characters in a file. The ‘wc’ stands for Word Count.
Linux wc Command
Let’s see how the wc command is defined by the man page. The man page can be accessed by typing in the following command.
The following will be displayed as output. Let’s understand the wc help page part by part.
The wc command as described can be used to get the number of newlines, words or bytes contained in a file specified.
The output will contain the number of newlines, words or bytes (file-wise, in case multiple files are inputted) followed by a “total” line in the end which will show the total sum of the words, newlines or bytes in all files.
Syntax of wc Command
$ wc [OPTION]… [FILE]…
Linux wc Command Example
Let’s see an example to understand wc command.
I have created 2 sample text files – test.txt and test2.txt on Desktop and navigated to the Desktop directory using the cd command.
Now, typing in the wc command according to the syntax and executing it, gives the following output.
The output has 3 lines corresponding to test.txt, test2.txt and total respectively.
- The first column in the output shows the number of lines in the file. Since there is only a single line in both the text files shown above, the output is 1 for test.txt and test2.txt.
- The second column in the output shows the number of words in the file, which in our case, is 12 and 15 respectively.
- The third column in the output displays the number of bytes + 1 (or characters since each character occupies 1 byte in memory,) in the file, which in our case, is 58 and 74 respectively in test and test2. (The +1 extra byte is due to ‘\n’ at the end of the line automatically being added by the text editor).
- The total line simply displays the sum of lines, words, and bytes in all files mentioned in the input.
Linux wc Command Options
The man page provides information about wc command options.
The following options, as shown above, can be used with the command to print specific counts – only what is required by the user.
- -c, –bytes option can be used to print the number of bytes+1 in a file as shown below. If multiple files are inputted, an extra total line is also displayed in the output.
- -m, –chars option can be used to print only the number of characters+1 in a file, as shown below. Since the number of characters equals the number of bytes in this example, the output is the same for both -c and -m.
- -l or –lines option can be used to print only the number of lines in a file, as shown below. If multiple files are provided, an extra total line is also displayed in the output.
- -L or –max-line-length option can be used to print the number of characters in the line with the maximum number of characters of all lines present in the file.
- -w or –words can be used to print only the number of words in a file, as shown below.
- –files0-from=F can be used to read input from files which have their names contained within a file F, separated by null-characters (byte value 0) and not whitespaces/tabs/endline. In case there is no file or file name is “-“, the input is read from the standard input directly.
Here, I have created 2 new .txt files – file1.txt and file2.txt containing names of some fruits and car companies respectively.
We can see that the output shows 10 for file1.txt and 13 for file2.txt. The lines “Watermelon” and “Maruti Suzuki” are the longest in their respective files and have 10 and 13 characters respectively.
You might notice that the total line seems to be displaying an incorrect sum. This is because when using -L, total displays the number largest among those displayed above it as output, which in our case is 13.
When – is written as file name and the command is executed, terminal waits for an input. If we type the file name then and press Ctrl+D twice, the output is displayed in the same line as shown. Once no more inputs are required, Ctrl+C is pressed to exit the process.
Using wc Command with cat and grep
Linux wc command can also take inputs directly from the standard input-output of another command can be redirected as input to wc. This is very useful for using it with other commands like cat and grep.
Here, the output from the cat was piped (redirected) from stdout to stdin of wc command. Output for test and test2 can also be shown in this way.
In this example, our goal is to find the total number of lines in file1.txt containing the letter “e”. For this, grep is used as shown, to filter out the letter “e” from the file. Then the output is piped (redirected) to the standard input of wc. We have added the -l option to wc in order to find the number of lines in the input.
Hence, our output comes out to be 4 as all names of fruits contain the letter “e” except Kiwi in the last line.
Linux wc command is simple, easy to use and can be used to search/filter out useful information from files by combining it with other commands like
grep. Tasks like finding number of lines containing certain alphabets in a huge text can be time-consuming when done manually. This command helps solve such problems in a matter of seconds.