wc Command
Beginner Text Processing man(1)Print newline, word, and byte counts for files
👁 10 views
📅 Updated: Mar 15, 2026
SYNTAX
wc [OPTION]... [FILE]...
What Does wc Do?
The wc (word count) command counts lines, words, characters, and bytes in files or input streams. Despite its name suggesting only word counting, it is most commonly used for counting lines.
wc is a fundamental tool in shell pipelines, often used to count results from grep, find, and other filtering commands. It provides quick answers to questions like "how many lines in this file?", "how many files match?", and "how large is this output?"
When given multiple files, wc shows counts for each file plus a total. This makes it useful for quick comparisons of file sizes and content volume across multiple files.
wc is a fundamental tool in shell pipelines, often used to count results from grep, find, and other filtering commands. It provides quick answers to questions like "how many lines in this file?", "how many files match?", and "how large is this output?"
When given multiple files, wc shows counts for each file plus a total. This makes it useful for quick comparisons of file sizes and content volume across multiple files.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -l | Count lines only | wc -l file.txt |
| -w | Count words only | wc -w essay.txt |
| -c | Count bytes only | wc -c binary.dat |
| -m | Count characters (multi-byte aware) | wc -m unicode.txt |
| -L | Print length of longest line | wc -L code.py |
| --files0-from | Read file list from a null-terminated input | find . -name '*.py' -print0 | wc -l --files0-from=- |
Practical Examples
#1 Count lines in a file
Shows the number of lines in the syslog file.
$ wc -l /var/log/syslog
Output:
15243 /var/log/syslog
#2 Count files in directory
Counts the number of files and directories in /var/log.
$ ls -1 /var/log | wc -l
Output:
47
#3 Count matching lines
Count error occurrences (grep -c is equivalent to grep | wc -l but faster).
$ grep -c "ERROR" app.log
Output:
23
#4 Count words in a document
Shows the total word count — useful for documents with word limits.
$ wc -w report.txt
Output:
2847 report.txt
#5 Count lines of code
Counts total lines of PHP code in the src/ directory.
$ find src/ -name '*.php' | xargs wc -l | tail -1
Output:
12450 total
#6 Compare file sizes
Shows line counts for each log file plus a total.
$ wc -l *.log
Output:
500 access.log\n 120 error.log\n 620 total
Tips & Best Practices
Count without filename: To get just the number: wc -l < file.txt (redirect instead of argument) or cat file | wc -l.
-c vs -m: -c counts bytes and -m counts characters. They differ for multi-byte encodings (UTF-8). Use -m for accurate character counts with non-ASCII text.
Trailing newline matters: wc -l counts newline characters. A file without a trailing newline will report one fewer line than you might expect.
Frequently Asked Questions
How do I count lines in a file?
Use wc -l filename. For just the number without the filename: wc -l < filename.
How do I count the number of files in a directory?
Use ls -1 /path | wc -l for files and directories. Use find /path -type f | wc -l for files only (including subdirectories).
What is the difference between wc -c and wc -m?
wc -c counts bytes, wc -m counts characters. For ASCII text they are the same. For UTF-8 text, multi-byte characters count as one character (-m) but multiple bytes (-c).
Related Commands
More Text Processing Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →