Terminal · foundation

Counting with wc

wc reports counts of lines, words, bytes, or characters from files or standard input according to the selected options and locale.

Why it matters

Counts are useful pipeline summaries, but bytes, characters, displayed columns, records, and human-language words are not interchangeable units.

Mental model

How to reason about counting with wc

wc scans the byte stream once and maintains requested counters, applying locale rules when identifying characters and whitespace-delimited words.

Analogy

It is an odometer with several modes: the same journey can be measured in raw steps, marked segments, or grouped stretches depending on the selected counter.

Examples

See the boundary, not just the happy path

Worked example · Count selected records

grep -F 'ERROR' app.log | wc -l

wc -l counts newline characters in grep's output, which corresponds to emitted lines when each selected record ends in a newline.

Worked example · Compare bytes and characters

printf 'é' | wc -c; printf 'é' | wc -m

In a UTF-8 locale, -c commonly reports two encoded bytes while -m reports one character, illustrating different measurement units.

Useful contrast · Missing final newline

printf 'one line' | wc -l

The result is zero because POSIX wc -l counts newline characters, not visually perceived nonempty text fragments.

Common mistakes

Misconceptions to remove early

Equating bytes with characters

Variable-width encodings can represent one character with multiple bytes; choose -c or -m based on the required unit.

Treating wc words as linguistic words

wc counts nonempty sequences delimited by whitespace, not language-aware tokens, punctuation rules, or identifiers.

Quick check

Can you predict the result?

1. What does wc -l count according to POSIX?
  • Newline characters
  • All nonempty visual rows
  • Sentence-ending punctuation
Answer: Newline characters
2. When can wc -c and wc -m differ?
Answer: When the character encoding uses more than one byte for at least one character.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster