Labs ICT
โญ Pro Login

Word Count

2 min read | Unix Tutorial
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

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.

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

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

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

๐Ÿงช Quick Quiz

What does the wc command do by default?