wc
Count lines, words, and bytes or characters
By CMD Script Team · 4 min read · Last updated
wc [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-l | Print only the newline (line) count |
-w | Print only the word count |
-c | Print only the byte count |
-m | Print only the character count (differs from -c in multibyte encodings) |
-L | Print 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 filewc -l file.txt— print only the line countwc -w file.txt— print only the word countwc -c file.txt— print only the byte count
wc -l /etc/passwd
Advanced examples
- Count how many files match a
findsearch:find . -name "*.py" | wc -l - Count matches from
grepwithout usinggrep -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 -lcounts "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 -cwhen you actually want character count on a UTF-8 file with multibyte characters —-ccounts bytes, which can be higher than the visible character count; use-mfor characters. - Piping through unnecessary intermediate commands, e.g.
cat file.txt | wc -l, whenwc -l file.txtdoes the same thing directly (the same "useless use of cat" pattern as withgrep). - Forgetting that
wcon 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, orlsto turn any list into a count:ls -1 | wc -lcounts entries in the current directory. wc -Lis 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 fileovergrep pattern file | wc -lwhen just counting matches — it avoids spawningwcas 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
-cvs-min 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 -candwc -m?-ccounts raw bytes;-mcounts characters. They're identical for ASCII text but differ on multibyte UTF-8 content, where-creports a higher number. - Why might
wc -lundercount the lines in a file?wc -lcounts 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 intowc -lto 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.