How can I count lines, words, or characters in a text file from the command line?
Quick answer
With GNU wc, use wc -l file.txt for newline characters, wc -w file.txt for whitespace-separated words, wc -m file.txt for characters, or wc -c file.txt for bytes. Combine options for several counts at once. Note that -l may not count a final line without a newline, and -m depends on the current locale. [S1]
The question, the details, the next step.
A practical checklist
Choose whether you need newline, word, character, or byte counts.
Run the matching
wcoption, or combine options for an overview.Use
-cwhen you need bytes rather than a locale-based character count.If the line total looks low, check whether the final line has a newline.
Confirm your platform’s
wcbehavior if results must match another tool.
A few more useful answers
Does `wc -l` count a final line that has no newline?
GNU wc -l counts newline characters, so it does not count a trailing partial line without a newline as another newline. [S1]
What is the difference between `wc -m` and `wc -c`?
-m reports characters according to the current locale; -c reports bytes. GNU wc does not count encoding errors in its character count. [S1]
Does `wc -w` use the same word definition as every word processor?
Not necessarily. GNU wc treats a word as a nonempty sequence separated by whitespace, and whitespace interpretation is affected by locale. [S1]
Sources
References used for this guide.
- [S1] GNU Coreutils 9.11: wc — Print newline, word, and byte counts gnu.org · Checked
