wc — Counting
Line, word, and byte counting — the measurement tool behind wc -l < file, find | wc -l, and file size checks.
Line Count — -l
Count lines in a file
wc -l /etc/passwd
Count lines from stdin — common in pipelines
grep -c "error" /var/log/syslog # grep -c is often better than grep | wc -l
Why grep -c beats grep | wc -l — one process instead of two
# Slower: spawns two processes, pipes between them
grep "pattern" file | wc -l
# Faster: grep counts internally
grep -c "pattern" file
Word Count — -w
Count words in a file
wc -w document.txt
Count words in a pipeline
echo "four words are here" | wc -w
Character Count — -m
Count characters (multibyte-aware)
wc -m document.txt
Difference between -m and -c matters for UTF-8
printf 'caf\xc3\xa9\n' | wc -m # 5 characters (e-acute is one char)
printf 'caf\xc3\xa9\n' | wc -c # 6 bytes (e-acute is two bytes)
Byte Count — -c
Count bytes — file size in bytes
wc -c binary.dat
Quick file size check without ls or stat
wc -c < /var/log/syslog # No filename in output when using stdin redirect
Max Line Length — -L
Find the longest line in a file — useful for format validation
wc -L /etc/passwd
Check if any lines exceed 80 characters — code style enforcement
wc -L script.sh # If output > 80, investigate:
awk 'length > 80 {printf "%4d (%d chars): %s\n", NR, length, $0}' script.sh
Multiple Files
Count lines in multiple files — wc totals them automatically
wc -l /var/log/syslog /var/log/auth.log /var/log/kern.log
Suppress the "total" line — useful in scripts
wc -l /var/log/*.log 2>/dev/null | awk '$2 != "total"'
Sort files by line count — find the biggest logs
wc -l /var/log/*.log 2>/dev/null | sort -rn | head -10
Combined with find/xargs — Project Statistics
Count total lines of code in a project — all .sh files
find ~/project -name "*.sh" -type f | xargs wc -l | tail -1
Lines of code per file — sorted by size
find ~/project -name "*.py" -type f -print0 | xargs -0 wc -l | sort -rn | head -20
Count non-blank, non-comment lines (effective lines of code)
find ~/project -name "*.sh" -type f -exec awk '/^[[:space:]]*$/{next} /^[[:space:]]*#/{next} {n++} END{printf "%6d %s\n",n,FILENAME}' {} \; | sort -rn
Count files by extension in a project
find ~/project -type f | awk -F'.' '{print $NF}' | sort | uniq -c | sort -rn
Total lines across all AsciiDoc files
find docs/ -name "*.adoc" -type f -print0 | xargs -0 wc -l | tail -1
Practical Patterns
Quick sanity check — did the filter reduce or increase line count?
echo "before:"; wc -l < data.txt
echo "after:"; grep -v '^#' data.txt | wc -l
Empty file check — zero lines means empty or missing content
find /etc/cron.d -type f -exec sh -c '[ "$(wc -l < "$1")" -eq 0 ] && echo "empty: $1"' _ {} \;
Count unique lines — combine with sort and uniq
sort data.txt | uniq | wc -l
wc with no flags — shows lines, words, bytes all at once
wc /etc/passwd
# Output: lines words bytes filename
Count arguments or items — wc -w on a single-line list
echo "$PATH" | tr ':' ' ' | wc -w # Number of PATH directories