$_ CLI In Depth
Learning progress 0%
lesson text viewing editors

Counting Lines, Words & Bytes with wc

5 min read Module 3: Viewing Files & Text Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:17

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.

BASH4 lines
cd /workspace
printf 'one two\nthree\n' > words.txt
wc words.txt
wc -l words.txt

The 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.

BASH3 lines
cd /workspace
wc -l < words.txt
ls | wc -l

The 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.

Tip

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.

How wc counts lines, words and bytes

Visualizer

wc makes one pass over the bytes. A line is a newline character, a word is a run of non-blank characters, and bytes are not characters.

1 / 5
h
i
␠
t
h
e
r
e
\n
o
k
\n
lines
0
words
0
bytes
0

Twelve bytes in total. wc starts with three counters at zero and an "inside a word" flag set to false.

printf "hi there\nok\n" | wc
Terminal lab · 3 steps · real Linux in your browser

Count lines, words and bytes

Count the lines, words and bytes of a small file and save each count.

01
Count lines
Save the number of lines in words.txt, without the file name: ``bash wc -l < words.txt > lines.txt ``
Show hint for step 1
Redirect the file into wc instead of naming it.
02
Count words
Save the number of words in words.txt: ``bash wc -w < words.txt > words-count.txt ``
Show hint for step 2
The -w option counts whitespace-separated words.
03
Count bytes
Save the number of bytes in words.txt: ``bash wc -c < words.txt > bytes.txt ``
Show hint for step 3
The -c option counts bytes, including the newlines.
Check yourself Question 1 of 4
Concept Check Option for bytes recall

Which wc option counts bytes?

Recap & Key Takeaways

wc counts newlines, words, and bytes, and -m counts characters in the current locale.
A line is terminated by a newline, so a final line without one may not be counted.
Redirecting input with < gives a bare number, which is convenient for scripts.
A byte count is a fast first check on a copy, but a checksum or cmp is needed for proof.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue