Labs ICT
โญ Pro Login

Word Count

Sometimes you just need to know how big something is. How many lines of code in your project? How many words in that essay? How large is this log file? The wc command โ€” short for "word count" โ€” answers all of these questions with one simple tool.

Basic word count

Run wc on a file and it gives you three numbers: lines, words, and bytes. In that order. It is surprisingly readable once you know what each column means.

$ wc report.txt
  42  312 18924 report.txt

That is 42 lines, 312 words, and 18,924 bytes.

Try it Yourself โ†’

Count lines with wc -l

The -l flag gives you just the line count. This is probably the most common variant. How many source files do you have? find . -name "*.js" | wc -l. How many errors in the log? grep "ERROR" server.log | wc -l.

$ wc -l server.log
1284 server.log

$ grep "ERROR" server.log | wc -l
17
Try it Yourself โ†’

Count words and characters

Use wc -w for word count and wc -c for byte count. On English text, byte count is roughly the same as character count, but not exactly if there are special characters or Unicode.

$ wc -w essay.txt
847 essay.txt

$ wc -c essay.txt
5284 essay.txt
Try it Yourself โ†’

Multiple files

wc can take multiple files and gives you a total at the end. Great for getting a quick summary of a whole project.

$ wc -l main.js utils.js
  84 main.js
 127 utils.js
 211 total
Try it Yourself โ†’

๐Ÿงช Quick Quiz

What does the wc command do by default?