Ubuntu

wc command: Explained with 5 examples in Ubuntu 20.04 LTS

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. Similarly, you can count the number of users on your system, the number of packages installed on your system, etc.

In this article, we will explain how to use wc command in Linux along with its command-line options and some examples.

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.

Syntax

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.

Basic usage of wc command

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 testfile.txt

Output explained:

The wc command 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 “testfile.txt”.

If you do not want to print the file name in the result, use < with the wc command as follows:

$ wc < testfile.txt

Count from multiple files

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.

Options

The wc command has some command-line options. Let’s have a look at those options and their functions:

Count the Number of Lines

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 “testfile.txt”:

$ wc -l testfile.txt

In the following output, 12 is the number of lines in the “testfile.txt”.

Count the Number of Words

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 “testfile.txt”:

$ wc -w testfile.txt

In the following output, 21 is the number of words in the “testfile.txt”.

Count the number of bytes

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 “testfile.txt”:

$ wc -c testfile.txt

In the following output, 116 is the number of bytes in the “testfile.txt”.

Count the Number of Characters

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 “testfile.txt”:

$ wc -m testfile.txt

In the following output, 116 is the number of characters in the testfile.txt.

Note: The byte count and character count are same for the files in ASCII format.

Print Length of the Longest Line

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 “testfile.txt”:

$ wc -L testfile.txt

In the following output, 20 is the length of the longest line.

View version

In order to view the version of wc command, use the following command:

$ wc --version

Print number of lines and bytes

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 the above output, 29 is the line count and 782 is the byte count in the file “sample.txt”.

Store count in a file

In order to save the output of wc command in a file, use the > operator as follows:

$ wc sample.txt > file_count

Wc Command Usage Example

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.

Count the number of users

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.

Count number of installed packages

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.

Count number of particular word in a file

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.txt”, the command would be:

$ grep -o ‘science’ sample.txt | wc -w

In this example, the grep command searches for the occurrence of the word “science” in the sample.txt file which is then piped to wc command to get a count.

Count number of characters in a specific line

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.txt”, the command would be:

$ head -50 sample.txt | tail -1 | wc -w

In this example, the head command will list the first 50 lines of the sample.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.

Use a Script for counting

In the following example, 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.

Create a script with the following lines of code:

#!/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

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.txt”, run the script by passing sample.txt as an argument:

$ ./script2 sample.txt

After running the script, you will receive the similar 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

This is how you can use the wc command in Linux. In this article, you have learned the basics of wc command along with its command-line options and few practical examples. Now you can easily count number of lines, words, characters and bytes in your files and in the output from other commands.

Similar Posts