The wc (word count) command is one of the GNU core utilities that apparently look simple but is actually quite a useful command. As apparent from the name, it counts the number of lines, words, characters, bytes, and even the length of the longest line in the file and prints them to standard output.
You may need this count for multiple reasons. For example, you can use the character count to restrict the number of characters that can be inserted in a post. Also, you can count the number of users on your system, the number of packages installed on your system, and so on.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers perform Linux commands related queries.
In this context, we shall look into how to use wc command in Linux along with its command-line options and some examples.
wc (short for word count) is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count.
We have performed the commands on Ubuntu 20.04 Focal Fossa system. More or less the same commands can also be implemented on other Linux distributions. We have run the commands on default Terminal application which can be accessed through Ctrl+Alt+T keyboard shortcut.
Here is the basic syntax for the wc command:
wc [OPTION]... [FILE]...
If you want to use multiple options, you can add them without a space. While for multiple files, you should separate them by a single space.
When wc command is used without any command-line options, it returns the number of lines, words, and bytes in the files. In order to use the wc command, simply type wc followed by the file name you want to find the count for.
$ wc newfile.txt
The output of the executed wc command will print the count results for the specified file in four columns. The first column shows the number of lines in the file, the second column shows the number of words, the third column shows the number of bytes and the fourth column shows the filename.
Note: wc only prints the newline counts not the actual lines in the specified file.
In the above command output, 12 is the number of lines, 21 is the number of words, and 116 is the number of bytes in the "newfile.txt".
If you do not want to print the file name in the result, use < with the wc command as follows:
$ wc < newfile.txt
With wc command, you can specify multiple files for the count. The wc command returns the count for each of the file along with the total count in the end which is the sum of the count for each file.
The wc command has some command-line options. Let’s have a look at those options and their functions.
Using wc command with -l or –lines option, you can print just the number of lines in the file. For instance, the following command will print only the line count in the "newfile.txt":
$ wc -l newfile.txt
In the output, 12 is the number of lines in the "newfile.txt".
If you need to print only the word count in a file, use the wc command with -w or –words option. For instance, the following command will print the word count in the "newfile.txt":
$ wc -w newfile.txt
In the output that follows, 21 is the number of words in the "testfile.txt".
Using wc command with -c or –bytes option, you can print the number of bytes in the file. The following command will print the byte count in the "newfile.txt":
$ wc -c newfile.txt
In the output that follows, 116 is the number of bytes in the "newfile.txt".
Using wc command with -m or –chars option, you can print the number of characters in the file. For instance, the following command will print only the character count in the "newfile.txt":
$ wc -m newfile.txt
In the output that follows, 116 is the number of characters in the newfile.txt.
Note: The byte count and character count are same for the files in ASCII format.
If you need to print length of the longest line in a specific file, you can do so using wc command with -L or –max-line-length option. For instance, the following command will print the length of the longest line of the "newfile.txt":
$ wc -L newfile.txt
In the output that follows, 20 is the length of the longest line.
In order to view the version of wc command, use the following command:
$ wc --version
You can also combine the command-line options. For instance, to print the line and byte count, combine -l and -c options as follows:
$ wc -lc
Note: Whether you run -lc or -cl, the output will always be in the same order i.e. line, word, and byte count.
In order to save the output of wc command in a file, use the > operator as follows:
$ wc sample.txt > file_count
Let's look at some of the examples of wc command by piping it with other commands.
Counting Files in any Directory
To count the number of files in any directory, pipe the output of find command to wc. For instance, to find the number of files in the ~/Downloads directory, the command would be:
$ find ~/Downloads/ -type f | wc -l
The find command lists all the files in the ~/Downloads directory of your system which is then piped to the wc command to get a count.
The wc command can also be used to count the total number of users in a system. To count the number of users, use the 'getent passwd' command and pipe its output to wc command as follows:
$ getent passwd | wc –l
The 'getent passwd' command list all the users in the system which is then piped to the wc command to get a count.
With the wc command, you can also count the number of installed packages in your system. To count the number of installed packages, use the following command:
$ apt list --installed | wc -l
The 'apt list –installed' command list all the installed packages in the system which is then piped to the wc command to get a count.
With the wc command, you can count number of particular word in a file. For instance, to find how many times the word “science” appeared in the file named "sample_file.txt", the command would be:
$ grep -o 'biology' sample_file.txt | wc -w
In this example, the grep command searches for the occurrence of the word "biology" in the sample_file.txt file which is then piped to wc command to get a count.
With the wc command, you can also find the number of characters in a specific line. For instance, to find the number of characters in line 50 of the file named "sample_file.txt", the command would be:
$ head -50 sample_file.txt | tail -1 | wc -w
Here, the head command will list the first 50 lines of the sample_file.txt file which is then piped to the tail command. The "tail -1" command then filters out the last line (Line No. 50) of the first output it obtained from the head command. The output from the tail command is then piped to the wc command to get a count of characters in line 50.
Here, we will create a script that will count lines, words, and bytes in a file. When running the script, we will pass the file name as an argument, so that you can run the same script for any of your files.
i. To Create a script with the following lines of code, execute the command:
#!/bin/bash
echo "File to read:"
read file
echo ""
echo "This file has the following number of:"
echo -n "Lines: "
wc -l < $file
echo -n "Words: "
wc -w < $file
echo -n "Chars: "
wc -c < $file
ii. Now run the script by passing the file name as an argument.
$ <./script_name> <path/to/filename>
For instance, to get the line, word, and character count for the file named "sample_file.txt", run the script by passing sample_file.txt as an argument:
$ ./script2 sample_file.txt
iii. After running the script, you will receive an output showing you the word count for your file.
If the file is in some other location other than your current working directory, then mention the full path:
$ ./script2 /etc/passwd