Menu
Coddy logo textTech

Word Count

Part of the Fundamentals section of Coddy's Terminal journey — lesson 31 of 82.

The wc command stands for word count. It counts the number of lines, words and characters in a file.

we have notes.txt:

Buy groceries from the store
Call the doctor tomorrow morning
Finish the terminal course today
Read the new book this weekend
Clean up the project folder

Simply type wc followed by the filename:

wc notes.txt

The output shows three numbers followed by the filename:

  5  30  159 notes.txt
  • First number — number of lines
  • Second number — number of words
  • Third number — number of characters

You can also use flags to get only the information you need:

-l — count only lines:

wc -l notes.txt

-w — count only words:

wc -w notes.txt

-c — count only characters:

wc -c notes.txt

Counting multiple files at once:

wc -l *.txt   # count lines in all .txt files

How does <strong>wc -l</strong> count lines?

wc -l counts newline characters ( ), not the number of visible lines. This means if your file has 5 lines but the last line has no newline at the end, wc -l will report 4 instead of 5.

This is standard Unix behavior — a proper text file should always end with a newline character on the last line. Most text editors add this automatically.

wc -l notes.txt
5 notes.txt   # correct: last line ends with newline

wc -l notes.txt
4 notes.txt   # missing newline at end of file

Common use cases:

  • Check how many lines are in a large log file
  • Count words in a document
  • Verify a file has the expected number of entries
challenge icon

Challenge

Beginner

Use the wc command to inspect notes.txt:

  1. Count the number of lines using wc -l
  2. Count the number of words using wc -w

Note: wc -l counts newline characters, so the file must end with a newline on the last line to get the correct count.

Cheat sheet

The wc command counts lines, words, and characters in a file:

wc notes.txt
  5  30  159 notes.txt
  • First number: lines
  • Second number: words
  • Third number: characters

Use flags for specific counts:

wc -l notes.txt   # count only lines
wc -w notes.txt   # count only words
wc -c notes.txt   # count only characters
wc -l *.txt       # count lines in all .txt files

Note: wc -l counts newline characters — if the last line has no newline at the end, the count will be one less than expected.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals