Basic usage
wc file.txt
wc -l file.txt
wc -w file.txt
wc -m file.txt
wc -c file.txt
Output format
wc file.txt
Multiple files
wc file1.txt file2.txt
wc *.txt
From stdin
ls -l | wc -l
ps aux | wc -l
find . -type f | wc -l
Just the number
wc -l < file.txt
wc -l file.txt | awk '{print $1}'
Count specific things
Lines
wc -l file.txt
grep -c . file.txt
grep -c "pattern" file.txt
Words
wc -w file.txt
tr -s ' ' '\n' < file.txt | sort | uniq | wc -l
Characters
wc -m file.txt
tr -d '\n' < file.txt | wc -m
Practical examples
Code statistics
find . -name '*.py' | xargs wc -l
find . -name '*.js' -o -name '*.jsx' | xargs wc -l | tail -1
Log analysis
grep "2025-12-09" app.log | wc -l
grep -i error syslog | wc -l
File comparison
echo "File 1: $(wc -l < file1.txt) lines"
echo "File 2: $(wc -l < file2.txt) lines"
Data validation
LINES=$(wc -l < data.csv)
if [ $LINES -ne 1000 ]; then
echo "Error: Expected 1000 lines, got $LINES"
fi
Advanced usage
Longest line
wc -L file.txt
Count file types
find . -type f | sed 's/.*\.//' | sort | uniq -c
Directory statistics
#!/bin/bash
echo "Files: $(find . -type f | wc -l)"
echo "Directories: $(find . -type d | wc -l)"
echo "Total lines: $(find . -name '*.txt' | xargs wc -l | tail -1)"
Comparison with alternatives
wc -l file.txt
grep -c '' file.txt
awk 'END {print NR}' file.txt
sed -n '$=' file.txt
Performance
time wc -l huge-file.log
time grep -c '' huge-file.log
time awk 'END {print NR}' huge-file.log
Common patterns
Progress indicator
TOTAL=$(wc -l < file.txt)
CURRENT=0
while read line; do
((CURRENT++))
echo "Processing $CURRENT/$TOTAL"
done < file.txt
Split file by line count
TOTAL=$(wc -l < file.txt)
HALF=$((TOTAL / 2))
head -n $HALF file.txt > first-half.txt
tail -n +$((HALF + 1)) file.txt > second-half.txt
Verify downloads
EXPECTED=10000
ACTUAL=$(wc -l < downloaded-file.csv)
if [ $ACTUAL -eq $EXPECTED ]; then
echo "Download complete"
else
echo "Incomplete: $ACTUAL/$EXPECTED lines"
fi
Script examples
Count summary
#!/bin/bash
FILE=$1
echo "=== File Statistics: $FILE ==="
echo "Lines: $(wc -l < "$FILE")"
echo "Words: $(wc -w < "$FILE")"
echo "Characters: $(wc -m < "$FILE")"
echo "Bytes: $(wc -c < "$FILE")"
Project statistics
#!/bin/bash
echo "=== Project Statistics ==="
echo "Total files: $(find . -type f | wc -l)"
echo "Python files: $(find . -name '*.py' | wc -l)"
echo "Python lines: $(find . -name '*.py' | xargs wc -l | tail -1 | awk '{print $1}')"
echo "JavaScript files: $(find . -name '*.js' | wc -l)"
echo "JavaScript lines: $(find . -name '*.js' | xargs wc -l | tail -1 | awk '{print $1}')"
Troubleshooting
Count doesn't match expected
cat -A file.txt | head
file file.txt
dos2unix file.txt
unix2dos file.txt
Large file hangs
head -n 10000 large-file.txt | wc -l
SAMPLE=$(head -n 10000 large-file.txt | wc -l)
SIZE=$(stat -c%s large-file.txt)
SAMPLE_SIZE=$(head -n 10000 large-file.txt | wc -c)
ESTIMATE=$((SAMPLE * SIZE / SAMPLE_SIZE))
echo "Estimated lines: $ESTIMATE"
Tips and tricks
wc -l < file.txt
ls -1 | wc -l
ps aux | wc -l
lsof -p PID | wc -l
netstat -an | wc -l
who | wc -l
dpkg -l | wc -l
git log --oneline | wc -l
Quick reference
wc file.txt
wc -l file.txt
wc -w file.txt
wc -m file.txt
wc -c file.txt
wc -l < file.txt
wc -l *.txt
command | wc -l
find . -name '*.py' | wc -l
ps aux | wc -l
grep "error" log | wc -l