>_cmd.script

wc

Count lines, words, and bytes or characters

Text Processing

By CMD Script Team · 4 min read · Last updated

SYNTAX
wc [OPTIONS] [FILE...]

Options

Command options and flags
FlagDescription
-lPrint only the newline (line) count
-wPrint only the word count
-cPrint only the byte count
-mPrint only the character count (differs from -c in multibyte encodings)
-LPrint only the length of the longest line

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

wc (word count) reads input and reports the number of lines, words, and bytes it contains, printing all three by default (in that order) followed by the filename. Any of the three can be requested individually with -l, -w, or -c/-m. It's a small, ubiquitous tool most often used at the end of a pipeline to turn a list of things (lines, matches, files) into a single count.

Beginner examples

  • wc file.txt — print line, word, and byte counts for a file
  • wc -l file.txt — print only the line count
  • wc -w file.txt — print only the word count
  • wc -c file.txt — print only the byte count
wc -l /etc/passwd

Advanced examples

  • Count how many files match a find search: find . -name "*.py" | wc -l
  • Count matches from grep without using grep -c: grep "ERROR" app.log | wc -l
  • Find the longest line in a script, useful for spotting overly long lines: wc -L script.sh
  • Compare byte count vs. character count on a UTF-8 file to detect multibyte content: wc -c file.txt; wc -m file.txt
  • Count total lines across multiple files, with a per-file and combined total: wc -l *.log
grep -c "ERROR" app.log   # vs.
grep "ERROR" app.log | wc -l   # equivalent result, different route

Common mistakes

  • Assuming wc -l counts "lines of text" when it actually counts newline characters — a file whose last line has no trailing newline will be undercounted by one.
  • Using wc -c when you actually want character count on a UTF-8 file with multibyte characters — -c counts bytes, which can be higher than the visible character count; use -m for characters.
  • Piping through unnecessary intermediate commands, e.g. cat file.txt | wc -l, when wc -l file.txt does the same thing directly (the same "useless use of cat" pattern as with grep).
  • Forgetting that wc on multiple files prints a "total" line at the end, which can throw off scripts that expect only per-file numbers.

Tips

  • Use wc -l < file.txt (with input redirection instead of a filename argument) to suppress the filename from the output when you only want the bare number.
  • Combine with find, grep, or ls to turn any list into a count: ls -1 | wc -l counts entries in the current directory.
  • wc -L is handy for enforcing line-length limits in CI, e.g. failing a check if any source line exceeds 100 characters.

Best practices

  • Prefer grep -c pattern file over grep pattern file | wc -l when just counting matches — it avoids spawning wc as an extra process for the same result.
  • When scripting, redirect input (wc -l < file) rather than passing a filename if you need a clean, unlabeled number for further processing.
  • Be explicit about -c vs -m in any script that processes non-ASCII text, since the two produce different numbers on multibyte content.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Quickly checking how many entries are in a log file before deciding how to process it: wc -l access.log.
  • Counting how many source files a project has as a rough size metric: find . -name "*.py" | wc -l.
  • Verifying a config or data file isn't empty before running a script against it: [ "$(wc -l < file.txt)" -gt 0 ] && process_file.

Common interview questions

  • What's the difference between wc -c and wc -m? -c counts raw bytes; -m counts characters. They're identical for ASCII text but differ on multibyte UTF-8 content, where -c reports a higher number.
  • Why might wc -l undercount the lines in a file? wc -l counts newline characters, so if the file's last line lacks a trailing newline, that line isn't counted.
  • How would you count how many files in a directory tree match a pattern? find . -name "*.ext" | wc -l — pipe the list of matching filenames into wc -l to get a count.

Frequently Asked Questions

What's the difference between wc -c and wc -m?

-c counts raw bytes, -m counts characters. For plain ASCII text they're identical, but in a UTF-8 file containing multibyte characters (accented letters, emoji, etc.), -c returns a higher number than -m since some characters take multiple bytes.

How do I count how many files match a pattern, not lines?

Pipe a listing into wc -l, e.g. find . -name '*.py' | wc -l counts matching files, and ls | wc -l counts entries in a directory. wc itself just counts lines of whatever text it receives.

Why does wc -l on a file without a trailing newline give a lower count than expected?

wc -l counts newline characters, not lines of visible text. If the last line isn't terminated with a newline, it won't be counted, so a 3-line file missing a final newline reports 2.

How do I count words in output from another command?

Pipe it into wc -w, e.g. cat essay.txt | wc -w, or more efficiently wc -w essay.txt directly if it's a file.

Cheat sheet

Download a quick-reference cheat sheet for wc.