What wc counts
By default wc prints three numbers: the number of newline characters, the number of words, and the number of bytes, followed by the file name if you gave one. Each option selects one count. The option -l counts lines, -w counts words, -c counts bytes, and -m counts characters. The option -L reports the length of the longest line.
A line, for wc, is the text terminated by a newline. This means a file whose last line lacks a newline has one fewer line than the number of lines a person would see. A word is a sequence of non-whitespace characters separated by whitespace, where whitespace includes spaces, tabs, and newlines. The definition is simple and practical, but it is not the same as a natural-language word, because punctuation and hyphens are ordinary characters.
cd /workspace
printf 'one two\nthree\n' > words.txt
wc words.txt
wc -l words.txtThe first command prints the counts for the file, which are two lines, three words, and fourteen bytes, followed by the name. The second prints only the line count, with the name.
Reading from standard input
When wc reads a file by name, it prints the name after the counts. When it reads standard input, it prints only the counts. This matters in scripts. If you redirect the input with < instead of naming the file, the output is just the number, which is convenient for capturing into a variable. The GNU version of wc may pad the numbers with spaces on some systems, so a script should strip spaces before comparing.
cd /workspace
wc -l < words.txt
ls | wc -lThe first command prints the number two with no file name. The second counts the entries in the current directory, which is a common idiom for quick counts.
Bytes are not characters
The option -c counts bytes, and -m counts characters. For ASCII text the two numbers are the same, because each character is one byte. For UTF-8 text they differ, because a character such as an accented letter takes two or more bytes. The option -m depends on the locale, so its result depends on the environment in which the command runs. Use -c when you need a stable number, and use -m when you truly want characters.
Counting in a pipeline
Most uses of wc are at the end of a pipeline. Filter the output with grep, then count the matches with wc -l. This pattern answers questions such as how many errors a log contains, or how many files match a pattern. Be aware that the count is only as good as the filter. A pattern that matches part of a word, or a case-sensitive search that misses a capital letter, gives a number that looks right and is wrong.
When you count matches, print a few of the matches as well. A count alone cannot show you that the pattern matched something unexpected.
Verifying a copy
Counting bytes is a quick way to check that a copy finished. Compare wc -c on the source and the destination. Equal byte counts are a good sign, but they are not proof, because two different files can have the same length. For a real comparison use a checksum or cmp, which the comparison unit covers. A byte count is a fast first check that catches truncated copies.
The same idea applies to line counts for logs. If a generator writes a fixed number of records, counting the lines verifies the number. If the count is short, the process probably stopped early, which is useful information during an incident.
grep -c and the other counts
To count matching lines, you can pipe grep into wc, or you can ask grep directly with grep -c. The two forms give the same number in the common case, and grep -c is simpler because it avoids a pipe. The difference appears when a pattern matches nothing: grep exits with status one, so a script that uses the count in a condition must consider the exit status as well as the printed number.
The option -L of wc prints the length of the longest line, which is a quick way to check whether a file has lines that might break a fixed-width consumer. It counts characters that are displayed, and it treats tabs as advancing to the next tab stop, so the number is a display width rather than a byte count.
Counting is an approximation of understanding. A count tells you how much there is, and the sample lines tell you what it is. Always look at a few lines of the input before you trust a number from a long pipeline, and keep the pipeline short enough that a reader can check each stage.
Counting in a log review
Suppose a service restarted during the night and you want to know how often it reported timeouts. A useful first pass is grep -c timeout service.log, which gives one number. A second pass uses grep timeout service.log | tail -n 5 to see the last few matching lines and their times. Only then do you decide whether the count is high or whether a single burst accounts for it. Counts and samples answer different questions, and the sample is the one that explains the count.
When the file is compressed, counting still works through a decompressor. The command zcat service.log.gz | wc -l streams the decompressed text into wc without writing a temporary file. The same approach works for any filter whose output you can produce as a stream, and it keeps large archives manageable on a small machine.
Practice in the lab
The lab creates a small file with two lines and three words and asks you to count each thing separately. Each answer goes into its own file, and the checker reads the number. Pay attention to the difference between reading from a name and reading from standard input.