Wc Command in Linux (Count Number of Lines, Words, Characters)

Introduction

Before we discuss wc command in linux, let's briefly understand-What is wc command ?

The wc command on Linux and Unix-like operating systems counts the number of lines, words, characters, and bytes in a file or standard input and prints the result.

It provides a simple way to analyze text content and gather important statistics regarding its structure and size. This utility is highly versatile and commonly used in shell scripts or as part of complex command pipelines.

We'll show you how to use the wc command with basic and useful examples in this tutorial.

How to Use the wc Command

The wc command has the following syntax:

wc OPTION... [FILE]...

The wc command can take zero or more FILE names as input. Wc will read the standard input if no FILE is supplied or if FILE is -. A word is defined as a sequence of characters separated by a space, tab, or newline.

When used without any options, the wc command prints four columns for each file supplied as an argument, including the number of lines, words, byte counts, and the file name. The fourth column (filename) is not visible when using the standard input.

The following command, for example, will show information about the virtual file /proc/cpuinfo:

wc /proc/cpuinfo 

You will get an output like below:

Output

218 6612 26751 /proc/cpuinfo
  • 218 is the number of lines.
  • 6612 is the number of words.
  • 26751 is the number of characters.

The file name is not displayed when using standard input:

wc < /proc/cpuinfo
Output

218 6612 26751

Pass the filenames as arguments, separated by a space, to display information about several files:

wc /proc/cpuinfo /proc/meminfo

The command outputs information on each file as well as a line with total statistics:

Output

218 6612 26751 /proc/cpuinfo
42 113 1783 /proc/meminfo
287 1275 21421 total

You can choose which counts are printed using the options below.

  • -l, --lines - Print the number of lines.
  • -w, --words - Print the number of words.
  • -m, --chars - Print the number of characters.
  • -c, --bytes - Print the number of bytes.
  • -L, --max-line-length - Print the length of the longest line.

When you have a lot of choices, Newline, words, characters, bytes, and maximum line length are printed in the following order: newline, words, characters, bytes, and maximum line length.

To display merely the number of words, for example, type:

wc -w /proc/cpuinfo
Output

3632 /proc/cpuinfo

Another example will print the number of lines as well as the longest line's length.

wc -lL /proc/cpuinfo
Output

448 792 /proc/cpuinfo

Wc can read input from files specified by NUL-terminated names in file F using the --files0-from=F option. Read names from standard input if F is -. For example, you can use the find command to look for files and then pass those files to wc:

find /etc -name 'host*' -printf '%f\0' | wc -l --files0-from=-

The number of lines for all files in the /etc directory with names that begin with "host" will be displayed in the output:

Output

4 /etc/host.conf
27 /etc/avahi/hosts
1 /etc/hostname
14 /etc/hosts
46 total

Count the Number of Lines

-l option with wc command is used to count only the number of lines in a text file. For example, to check the number of lines in the /etc/passwd run the following command:

wc -l /etc/passwd 

The number of lines is in the first column, and the file name is in the second:

Output

44 /etc/passwd

Count the Number of Words

Use wc -w followed by the file name to count only the number of words in a text file. The example below counts the amount of words in the file ~/Documents/file.txt:

wc -w /etc/passwd 

In the first column, the number of words is displayed:

513 /home/vegastack/Documents/file.txt

Wc Command Examples

Using piping, the wc command can be used in conjunction with other commands. Here are a few examples of what I'm talking about.

Counting Files in the Current Directory

The find command sends a single-line list of all files in the current directory to the wc program, which counts the number of lines and outputs the result:

find . -type f | wc -l

Count the number of users

The wc command is used to count the number of lines in the output of the getent command in the example below.

getent passwd | wc -l

FAQs on Wc Command in Linux

How can I count the number of words in a file?

Use the command wc -w <file> to count the number of words in a file.

How do I count the number of characters in a file?

Use the command wc -c <file> to count the number of characters (including white spaces) in a file.

Can I count the number of characters excluding white spaces?

Yes, you can count the number of characters excluding white spaces using the command wc -m <file>.

How can I count the number of lines, words, and characters together?

Use the command wc <file> to count the number of lines, words, and characters in a file. The output will be displayed in the order lines, words, characters.

Can I count multiple files at once?

Yes, you can count multiple files simultaneously by specifying their names after the wc command, e.g., wc file1.txt file2.txt.

How do I count the total number of lines, words, and characters in multiple files?

Use the command wc -lwc <file1> <file2> to get the total count of lines, words, and characters in multiple files.

How can I count the lines, words, and characters of input received from a command or pipe?

You can use the wc command without specifying a file name, and it will consider the input received from a command or pipe, e.g., echo "Hello World" | wc.

Conclusion

The wc command, which stands for "word count," has a straightforward syntax. It can count the lines, words, bytes, and characters in a single or several text files.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.