Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

wc Command Guide for Counting Lines, Words, and Characters

This guide helps you understand and use the wc command, the standard way to count how much text is in a file or a stream.


What is wc, and Why Use It?

The wc command (“word count”) reads a file or stream and reports how many lines, words, characters, and bytes it contains. It’s the smallest useful text tool there is — one number that tells you whether a log is empty, whether a config is truncated, or how big a translation job just became.

Why Learn wc?

  • Quick sanity checks: “How many lines did that grep match?” is grep ... | wc -l.
  • Scripting arithmetic: wc -l returns a plain number, perfect for if checks and loops.
  • It’s the end of every pipeline: Counting is the natural last step after filtering and sorting.
  • Zero setup: It’s in every POSIX system and needs no flags for the basics.

wc never tells you what is in the text — only how much of it there is. Pair it with grep to find things first.


1. Count Everything at Once

wc file.txt
  • With no flags, wc prints three numbers: lines, words, characters, then the filename.
42 317 2048 file.txt
  • That file has 42 lines, 317 words, and 2048 characters. One glance tells you the file is small.

2. Count Lines Only

wc -l file.txt
  • -l prints just the line count — the most common form by far.

The real use is at the end of a pipeline:

grep "ERROR" app.log | wc -l
  • One number: how many lines matched. This is how you count errors, users, entries, or anything else in a stream.

3. Count Words and Characters

wc -w essay.txt
  • -w counts words — runs of characters separated by whitespace.
wc -c essay.txt
  • -c counts bytes, not characters. For plain ASCII they’re the same, but for UTF-8 text (accents, emoji, CJK) the byte count is higher than the character count.
wc -m essay.txt
  • -m counts characters correctly, even when each character is multiple bytes. Use -m whenever the file contains non-ASCII text.

4. Count Several Files

wc file1.txt file2.txt file3.txt
  • wc prints one row per file, then a total row at the bottom.
wc *.conf
  • Shell globbing (*.conf) plus multi-file counting gives you a per-file breakdown of a whole directory in one command.

5. Count What find or ls Produced

find . -name "*.py" | wc -l
  • Counts every Python file under the current directory. The pipeline feeds find’s output into wc as a plain stream.
ls | wc -l
  • Counts entries in the current directory. Careful: this counts lines of ls output, which wraps oddly on narrow terminals — find . -maxdepth 1 | wc -l is more reliable.

6. Combine with the Classic Pipeline

The word-count pipeline from the section README, explained:

cat file.txt | tr ' ' '\n' | sort | uniq -c | sort -rn | head -10
  • tr puts one word per line, sort groups them, uniq -c counts each. wc is the simpler cousin of uniq -c: wc -w counts all words, uniq -c counts each distinct word.

Common Pitfalls

  • wc -l counts newlines, not “visual lines”. A file whose last line has no trailing newline reports one fewer line. grep -c "" file is a more reliable line count for such files.
  • wc -c counts bytes, not characters. Multibyte UTF-8 text inflates the count; use -m for real character counts.
  • wc -w splits on whitespace only. “don’t” is one word, but “3.14” counts as one word and “e-mail” as one — word boundaries are a blunt instrument.
  • wc on a stream with no filename prints no name. That’s fine for pipes, but don’t expect the filename to help you identify output when counting many things at once — pass files explicitly instead.
  • Counting with ls | wc -l can lie. Filenames with newlines break the count. Prefer find ... -print | wc -l or find ... -print0 | tr -dc '\n' | wc -c for exactness.

PageRelationship
grepFinds the lines wc then counts.
sortGroups lines so uniq -c can count them.
uniqCounts each distinct line instead of totals.
trSplits text into words for counting pipelines.

wc Cheatsheet

CommandWhat It Does
wc fPrint lines, words, and characters.
wc -l fCount lines only.
wc -w fCount words only.
wc -c fCount bytes.
wc -m fCount characters (UTF-8 safe).
wc *.txtPer-file counts plus a total.
grep x f | wc -lCount matching lines.
find . -name "*.py" | wc -lCount files of a type.