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.txtThe 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.txtCounting multiple files at once:
wc -l *.txt # count lines in all .txt filesHow 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 fileCommon 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
BeginnerUse the wc command to inspect notes.txt:
- Count the number of lines using
wc -l - Count the number of words using
wc -w
Note:
wc -lcounts 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 filesNote: 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline