In UNIX-like operating systems, we predominantly use the shell to execute various Linux commands to accomplish common tasks. The WC command is one of the prevalent commands utilized to retrieve information related to the contents of a text file.
In this tutorial, we learn about wc command and its options with examples.
The WC command is used to get the number of lines, word count, characters count, or byte counts in the specified file, multiple files, or standard input. The command name WC is shortened for word count. wc comes most useful in bash programming to find file content stats.
The WC command presents its output in a table format which consists of four columns. Each column shows the number of lines, words, bytes, and the name of the input object specified as a file argument. The line count includes empty lines as well.
wc [Options]... [File]...
The options and file name arguments are optional to the WC command. When specified, the command accepts multiple options and files as arguments. If no File argument is specified, the WC command reads from the standard input.
When you use the WC command without any options, by default, it will print four columns starting from the count of lines, words, characters, and finally the file names specified in the File arguments list. If the command is reading from standard input, the fourth column will be dropped.
Useful options of wc command:
|-l||It prints the number of lines in the given file or standard input.|
|-w||The word counts of the specified file or files will be printed.|
|-m||The number of bytes contained in the specified file or files will be printed.|
|-c||The byte count of a given file/s will be the output.|
|-L||Find the longest lines present in the input and displays their length.|
wc options Examples
The WC command accepts five major optional arguments along with one or more than one file name. When multiple options specified, the output will be in the order of count of lines, words, characters, bytes, and maximum line length.
We can use the -l option to count only the number of lines of a given file as follows.
wc -l continents.txt
Let's specify two files and count the number of lines in each file.
wc -l continents.txt oceans.txt
The -w option can be used to get only the number of words present in a file as shown in the following command.
wc -w continents.txt
Similarly, more than one file can be specified in the command to count the number of words in each file separately.
When you need to count only the number of bytes in a given file, the -c option can be used.
wc -c continents.txt
Let's execute the same command by specifying multiple files as follows.
wc -c continents.txt oceans.txt
The -m argument can be specified to retrieve only the character count in a single file as shown in the following command.
wc -m continents.txt
For multiple files, the command would look as follows.
wc -m continents.txt oceans.txt
The -L option is used to find the length of the longest line contained in a given file. In this example, the continents.txt contains the line "Main Continents in the Earth." which contains 29 characters. Hence it is the longest line present in this particular file.
wc -L continents.txt
It is possible to get the maximum length of the longest line contained in multiple files using the -L option. The maximum line length will be displayed separately for each file specified. In this case, the extra line total won't display, instead, the maximum line length among all the specified files will be displayed.
wc -L continents.txt oceans.txt
Some Practical Examples
The wc command can be piped with other commands to accomplish regular tasks listed in the following.
1. Count the number of times a word appears in a file
We can count the number of times a given word appeared in a file as follows.
cat continents.txt | grep -o 'America' | wc -l
First, we get the contents of the specified file using the cat command and pipe the output to the grep command in search of the specified word. Finally, this output is piped to the wc command with the -l option which counts only the number of words.
Since the word 'America' appeared in two places of the continents.txt file, the output is 2.
2. Count the number of files and directories in current directory
In some cases, we need to count the number of files and directories that reside in the current directory. It is quite straightforward to use the wc command along with the ls command.
ls -1 | wc -l
you can use the following command to count only the number of files residing in the current directory. In this example, we first find the files by type and pipe the results into the wc command.
find . -type f | wc -l
3. Count the total number of users
The wc command is also used to count the number of users in databases like group and passwd where all the user information is stored.
getent passwd | wc -l
To conclude, the command line command WC which stands for word count used to count the number of lines, words, characters, bytes, and the number of characters contained in the longest line in a given file or multiple files.
Overall, the wc command can be used to count different things in text files and the functionality can be extended by piping with other shell commands.
Thanks for reading, please leave your feedback and suggestions in the comment below.