COUNTING WITH WC

Linux comes with a very small, yet handy utility called wc (word count), which is used to count words, lines, characters, and bytes in a file. It can also use standard input as the text to count. To use wc, simply provide it with the option that tells it what you want counted. For instance, to count the number of words in the file ~/myfile.txt, use:

# wc -w ~/myfile.txt

To count the number of lines, use the "-l" option. For bytes and characters, use "-c" or "-m" respectively. The byte count is not as helpful as the others, as you can obtain the same information from ls. If you were to use:

# wc -c ~/myfile.txt

and then:

# ls -l ~/myfile.txt

you would see that the byte count is identical. However, wc has many other uses. Let's assume that you have a flat text file full of e-mail addresses and want to know how many are in the file. If each e-mail address is on its own line, you can use the following to obtain an accurate number of e-mail addresses contained in the file:

# wc -l ~/email.list

Or, if a program outputs a list of e-mail addresses and you want to count the number it contains, you can use the following, and wc would provide the number of entries that the program printed to standard output:

# /usr/bin/myprogram | wc -l