The wc Command

wc stands for "word count," but it does much more.

Basic Usage

Terminal
$wc file.txt
42 256 1847 file.txt

Three numbers:

  1. 42 - lines
  2. 256 - words
  3. 1847 - bytes (characters)

Count Lines Only

Terminal
$wc -l file.txt
42 file.txt

This is by far the most common use of wc.

Quick Line Count

"How many lines in this file?" โ†’ wc -l file "How many files in this directory?" โ†’ ls | wc -l

Count Words Only

Terminal
$wc -w file.txt
256 file.txt

Count Characters

Terminal
$wc -c file.txt
1847 file.txt
$wc -m file.txt
1842 file.txt
  • -c = bytes
  • -m = characters (matters for Unicode)

Multiple Files

Terminal
$wc -l *.txt
42 notes.txt 156 readme.txt 89 todo.txt 287 total

Counts each file plus a total.

Combined with Pipes

Terminal
$cat /etc/passwd | wc -l
42
$grep 'ERROR' app.log | wc -l
15
$find . -name '*.js' | wc -l
127

Counting Results

Pipe anything to wc -l to count results:

  • How many errors? grep 'ERROR' log | wc -l
  • How many JavaScript files? find . -name '*.js' | wc -l
  • How many processes? ps aux | wc -l

Real-World Examples

Lines of Code

Terminal
$find . -name '*.py' -exec cat {} \; | wc -l
4523

Users on System

Terminal
$wc -l /etc/passwd
42 /etc/passwd

Running Processes

Terminal
$ps aux | wc -l
156

(Subtract 1 for the header line)

Log Entries Today

Terminal
$grep "$(date +%Y-%m-%d)" app.log | wc -l
892
Knowledge Check

How do you count the number of lines in a file?

Quick Reference

FlagCounts
-lLines
-wWords
-cBytes
-mCharacters

Key Takeaways

  • wc = word count (lines, words, bytes)
  • -l is most commonly used for line counts
  • Pipe output to wc -l to count anything
  • Works on multiple files and shows totals
  • Essential for quick statistics on files and command output

Next: character translation with tr.