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
grepmatch?” isgrep ... | wc -l. - Scripting arithmetic:
wc -lreturns a plain number, perfect forifchecks 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.
wcnever tells you what is in the text — only how much of it there is. Pair it withgrepto find things first.
1. Count Everything at Once
wc file.txt
- With no flags,
wcprints 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
-lprints 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
-wcounts words — runs of characters separated by whitespace.
wc -c essay.txt
-ccounts 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
-mcounts characters correctly, even when each character is multiple bytes. Use-mwhenever the file contains non-ASCII text.
4. Count Several Files
wc file1.txt file2.txt file3.txt
wcprints one row per file, then atotalrow 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 intowcas a plain stream.
ls | wc -l
- Counts entries in the current directory. Careful: this counts lines of
lsoutput, which wraps oddly on narrow terminals —find . -maxdepth 1 | wc -lis 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
trputs one word per line,sortgroups them,uniq -ccounts each.wcis the simpler cousin ofuniq -c:wc -wcounts all words,uniq -ccounts each distinct word.
Common Pitfalls
wc -lcounts newlines, not “visual lines”. A file whose last line has no trailing newline reports one fewer line.grep -c "" fileis a more reliable line count for such files.wc -ccounts bytes, not characters. Multibyte UTF-8 text inflates the count; use-mfor real character counts.wc -wsplits 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.wcon 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 -lcan lie. Filenames with newlines break the count. Preferfind ... -print | wc -lorfind ... -print0 | tr -dc '\n' | wc -cfor exactness.
Related Pages
| Page | Relationship |
|---|---|
grep | Finds the lines wc then counts. |
sort | Groups lines so uniq -c can count them. |
uniq | Counts each distinct line instead of totals. |
tr | Splits text into words for counting pipelines. |
wc Cheatsheet
| Command | What It Does |
|---|---|
wc f | Print lines, words, and characters. |
wc -l f | Count lines only. |
wc -w f | Count words only. |
wc -c f | Count bytes. |
wc -m f | Count characters (UTF-8 safe). |
wc *.txt | Per-file counts plus a total. |
grep x f | wc -l | Count matching lines. |
find . -name "*.py" | wc -l | Count files of a type. |