The wc Command

The wc (i.e., word count) command by default counts the number of lines, words and characters in text.

wc defines a word as a set of contiguous letters, numbers and/or symbols which are separated from other characters by one or more spaces, tabs and/or newline characters (which are generated when the RETURN key is pressed). When counting the number of characters, all characters are counted, not only letters, numbers and symbols, but also spaces, tabs and newline characters. A line is only counted if it ends with a newline character.

wc's syntax is

wc [options] [file_name(s)]

The items in square brackets are optional. If no file names are provided, wc reads from its standard input, which by default is text entered at the keyboard.

This can be seen by typing


at the command line (i.e., in the all-text mode), pressing the ENTER key to move to a new line and then typing some text on one or more lines. The command is executed (i.e., run) by pressing the ENTER key again and then pressing the CONTROL and d keys simultaneously. This causes wc to write in a new line (under the lines of text) its count of the numbers of lines, words and characters in the text.

The following command counts the number of lines, words and characters in a file named file1 that resides in the current directory (i.e., the directory in which the user is currently working) and likewise writes them, followed by the name of the file, to standard output, which is by default the display monitor:

wc file1

wc can provide its output for multiple files by listing the name of each separated by a space. For example,

wc file1 file2 file3

The numbers of lines, words and characters for each file along with its name will be displayed on a separate line and in the order that the files are listed as arguments (i.e., input files). In the case of multiple arguments such as this, wc also provides an additional line that shows the total number of lines, words and characters for all the files.

Likewise, wc can provide a count for all of the text files within a directory. This is accomplished by using the star wildcard character, which represents everything and is designated by an asterisk ( * ). For example, the following will display the number of lines, words and characters for each file in the current directory (which is represented by a dot) as well as totals for all files in the directory:

wc . *

wc has only a few options, the most commonly used of which restrict the information it provides. The -l option tells wc to count only the number of lines, the -w option tells it to count only the number of words, the -m option tells it to count only the number of characters and the -c option tells wc to count only the number of bytes.

Thus, for example, the following displays just the number of words in a file named file4:

wc -w file4

The following displays the number of characters in the same file:

wc -m file4

As is generally the case with commands in Unix-like operating systems, any combination of options can be used together. For example, the following would count both the numbers of lines and words in a file named file5:

wc -lw file5

Redirection can be used with wc to create more complex commands. For example, the output from the above command can be redirected using the standard output redirection operator (which is designated by a rightward pointing angle bracket) from the display screen to a file named file6 with the following:

wc -lw file5 > file6

If file6 already exists, its contents will be overwritten; if it does not exist, it will be created. The contents of file6 can be easily confirmed with a text editor or with a command such as cat, which is commonly used to read text files, i.e.,

cat file6

As another example, wc with its -l option can be used to count the total number of objects (i.e., files, links and directories) in the current directory. This is accomplished by first employing the ls command, which by default lists the contents of the current directory, and then sending its output via a pipe (designated by the vertical bar character) to wc, which counts the number of lines in the output from ls, i.e.,

ls | wc -l

wc can count the words, lines and/or characters only in text files. However, when used with its -c option, it can count the bytes in any type of file, i.e. a plain text file or a binary file. (A binary file is any file that contains at least some non-text data, such as an image file, a compressed text file or an executable program.) This can be a convenient way of determining the size of a file.

For example, the following command displays the size for each jpeg image file in the current directory as well as the total for all of them:

wc -c *.jpg

Created June 2, 2004. Last updated May 24, 2006.
Copyright © 2004 - 2006 The Linux Information Project. All Rights Reserved.