wc Cheatsheet
Quick reference for counting lines, words, bytes, characters, and longest line length with wc in Linux
The `wc` command counts lines, words, bytes, and characters in files or command output. This cheatsheet covers the most common counting patterns you will use in scripts and daily shell workflows.
Basic Syntax
Core wc command forms.
| Command | Description |
|---|---|
wc file.txt | Show lines, words, and bytes for a file |
wc file1 file2 | Show counts per file and a total line |
wc *.log | Count all matching files |
command | wc | Count output from another command |
wc --help | Show available options |
Common Count Flags
Use flags to request specific counters.
| Command | Description |
|---|---|
wc -l file.txt | Count lines only |
wc -w file.txt | Count words only |
wc -c file.txt | Count bytes only |
wc -m file.txt | Count characters only |
wc -L file.txt | Show longest line length |
Useful Pipelines
Practical wc combinations for quick shell checks.
| Command | Description |
|---|---|
ls -1 | wc -l | Count directory entries (one per line) |
grep -r "ERROR" /var/log | wc -l | Count matching log lines |
find . -type f | wc -l | Count files recursively |
ps aux | wc -l | Count process list lines |
cat file.txt | wc -w | Count words from stdin |
Multi-File Counting
Summarize multiple files and totals.
| Command | Description |
|---|---|
wc -l *.txt | Line count for each file plus total |
wc -w docs/*.md | Word count per Markdown file and total |
wc -c part1 part2 part3 | Byte count per file and combined total |
wc -m *.csv | Character count for each CSV file |
wc -L *.log | Max line length per file and max total |
Script-Friendly Patterns
Extract numeric output safely in scripts.
| Command | Description |
|---|---|
count=$(wc -l < file.txt) | Capture pure line count without filename |
words=$(wc -w < file.txt) | Capture word count only |
bytes=$(wc -c < file.txt) | Capture byte count only |
if [ "$(wc -l < file.txt)" -gt 1000 ]; then ... fi | Threshold check in scripts |
printf '%s\n' "$text" | wc -m | Count characters in a variable |
Troubleshooting
Quick checks for common wc confusion.
| Issue | Check |
|---|---|
| Count includes filename | Use input redirection: wc -l < file |
| Unexpected word count | Confirm whitespace and delimiters in the file |
| Character and byte counts differ | Use -m for characters and -c for bytes |
| Total line missing with many files | Ensure shell glob matches at least one file |
| Pipeline count seems off by one | Some commands add headers; account for that |
Related Guides
Use these guides for complete text-processing workflows.
| Guide | Description |
|---|---|
| linux wc Command | Full wc guide with detailed examples |
| head Command in Linux | Show first lines of files |
| tail Command in Linux | Show last lines and follow logs |
| grep Command in Linux | Search and filter matching lines |
| find Files in Linux | Build file lists for counting |