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

Updated 

4 min read

On Linux and Unix-like operating systems, the wc command allows you to count the number of lines, words, characters, and bytes of each given file or standard input and print the result.

In this tutorial, we will show you how to use the wc command through simple and practical examples.

How to Use the wc Command

The syntax for the wc command is as follows:

wc OPTION... [FILE]...

The wc command can accept zero or more input FILE names. If no FILE is specified, or when FILE is -, wc will read the standard input. A word is a string of characters delimited by a space, tab, or newline.

In it’s simplest form when used without any options, the wc command will print four columns, the number of lines, words, byte counts and the name of the file for each file passed as an argument. When using the standard input the fourth column (filename) is not displayed.

For example, the following command will display information about the virtual file /proc/cpuinfo:

wc /proc/cpuinfo 

The output will look something like the following:

448 3632 22226 /proc/cpuinfo
  • 448 is the number of lines.
  • 3632 is the number of words.
  • 22226 is the number of characters.

When using the standard input, the file name is not shown:

wc < /proc/cpuinfo
448 3632 22226

To display information about more than one file, pass the filenames, as arguments, separated by space:

wc /proc/cpuinfo

The command will give you information about each file and a line including total statistics:

448 3632 22226 /proc/cpuinfo
49 143 1363 /proc/meminfo
497 3775 23589 total

The options below allow you to select which counts are printed.

  • -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 using multiple options counts are printed in the following order: newline, words, characters, bytes, maximum line length.

For example, to display only the number of words you would use:

wc -w /proc/cpuinfo
3632 /proc/cpuinfo

Here is another example that will print the number of lines and the length of the longest line.

wc -lL /proc/cpuinfo
448 792 /proc/cpuinfo

The --files0-from=F option allows wc to read input from the files specified by NUL-terminated names in file F. If F is - then read names from standard input. For example, you can search for files using the find command and provide those files as an input to wc:

find /etc -name 'host*' -printf0 | wc -l --files0-from=-

The output will show the number of lines for all files in the /etc directory whose names start with “host”:

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

Count the Number of Lines

The wc command is mostly used with the -l option to count only the number of lines in a text file. For example, to count the number of lines in the /etc/passwd file you would type:

wc -l /etc/passwd 

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

44 /etc/passwd

Count the Number of Words

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

wc -l /etc/passwd 

The number of words is shown in the first column:

513 /home/linuxize/Documents/file.txt

Wc Command Examples

The wc command can be used in combination with other commands through piping. Here are a few examples.

Counting Files in the Current Directory

The find command passes a list of all files in the current directory with each file name on a single line to the wc command, which counts the number of lines and prints the result:

find . -type f | wc -l

Count the number of users

In the example below wc is used to count the number of lines from the output of the getent command .

getent passwd | wc -l

Conclusion

The wc command stands for “word count” and has a quite simple syntax. It allows you to count the number of lines, words, bytes, and characters in one or multiple text files.

If you have any questions or feedback, feel free to leave a comment.