×


wc command: Demonstrated with 5 examples on 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. 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.

More about wc command in Linux ?

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.




wc command 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 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


How to Count from multiple files with wc command?

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.

wc command 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 "newfile.txt":

$ wc -l newfile.txt

In the output, 12 is the number of lines in the "newfile.txt".

How to Count the Number of Words with wc command ?

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".

How to Count the number of bytes with wc command ?

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".

How to Count the Number of Characters with wc command?

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.

How to Print Length of the Longest Line with wc command?

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.

How to view wc command version?

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

$ wc --version

How to Print number of lines and bytes with wc command?

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.

How to Store count in a file with wc command ?

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_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.

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_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.

Use a Script for counting

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


[Need urgent assistance in fixing Debian and Ubuntu related errors? We are available to help you. ]


Conclusion

This article will guide you on how to use the #wc #command in #Linux. Here you will learn 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. The wc command in #UNIX is a command line utility for printing newline, word and byte counts for files. It can return the number of lines in a file, the number of characters in a file and the number of words in a file. It can also be combine with pipes for general #counting operations. To use use WC command, you need to know the following: wc -l : Prints the number of lines in a file. wc -w : prints the number of words in a file. wc -c : Displays the count of bytes in a file. 1. A Basic Example of WC Command. 2. Count Number of Lines. 3. Display Number of Words. 4. Count Number of Bytes and Characters. 5. Display Length of Longest Line.