shell · difficulty ◆
wc — count lines, words, and characters
How many lines? How many words? How big is it really? wc answers all three.
$ wcWhat it does
wc (word count) reads input — a file or stdin — and reports how many lines, words, bytes, and characters it contains. By default it prints all three counts at once, in the order lines, words, bytes. Because it accepts a filename or reads from a pipe, it slots into almost any shell one-liner where you need a quick measurement.
Why it matters
wc is the universal measuring stick of the Unix text toolkit. It powers things like 'how many lines did this log have?', 'how many source files match this pattern?', and 'how many words is this document?' when piped after other commands. It is the natural partner of sort, uniq, and grep for turning raw text into concrete numbers.
Example
$ wc README.md$ wc README.md\n 42 198 1320 README.md\n\n$ wc -l README.md\n42 README.mdDefault output is lines, words, bytes. -l narrows it to just the line count (42).
$ grep -c 'error' server.log | wc -l$ grep 'error' server.log | wc -l\n17wc reads from stdin when no file is given — here it counts how many lines grep returned.
Common flags
- -l
- count newlines (lines) only
- -w
- count words only
- -c
- count bytes only (the raw size of the file)
- -m
- count characters instead of bytes (differs for multi-byte UTF-8)
- -L
- print the length of the longest line
- -lw
- combine flags — e.g. lines and words together
History
Origin
wc has been part of Unix since the very first edition in 1971, making it one of the oldest surviving commands. Its name is simply an abbreviation of 'word count'. Early Unix designers built a small set of single-purpose text tools like this on purpose — they expected you to combine them with pipes rather than build one giant program.
Fun fact
Despite its age, wc is still the fastest way to answer 'how many lines is this file?' The classic `wc -l` is a favorite way to gauge the size of a codebase, and it is the tool behind the famous 'lines of code' counters used in programming surveys.