wc (Word Count)
Count lines, words, characters, and bytes in files
Syntax:
wc [options] [file...]
Note: wc displays newline, word, and byte counts for each file. If no files are specified, it reads from standard input.
Description
The wc (word count) command is used to count the number of lines, words, characters, and bytes in files. It's a fundamental text processing tool that provides quick statistics about text files. By default, wc displays line count, word count, and byte count.
Command Options
| Option | Description |
|---|---|
-l, --lines |
Print only the newline count |
-w, --words |
Print only the word count |
-c, --bytes |
Print only the byte count |
-m, --chars |
Print only the character count |
-L, --max-line-length |
Print the length of the longest line |
--files0-from=F |
Read input from files specified in file F |
Basic Usage
Default output (lines, words, bytes):
# Count lines, words, and bytes in a file wc filename.txt # Output: 25 150 800 filename.txt # lines words bytes filename # Count from standard input echo "Hello world" | wc # Output: 1 2 12
Specific Count Types
Count lines only:
# Count lines in a file wc -l filename.txt # Count lines in multiple files wc -l file1.txt file2.txt file3.txt # Count lines from command output ps aux | wc -l
Count words only:
# Count words in a file wc -w document.txt # Count words from input echo "This is a test sentence" | wc -w # Output: 5
Count characters and bytes:
# Count characters (multibyte aware) wc -m filename.txt # Count bytes wc -c filename.txt # Compare character vs byte count for UTF-8 file wc -m utf8file.txt # Character count wc -c utf8file.txt # Byte count (may be larger)
Advanced Usage
Longest line length:
# Find the longest line in a file wc -L filename.txt # Find longest line in multiple files wc -L *.txt
Multiple files:
# Count statistics for multiple files
wc file1.txt file2.txt file3.txt
# Shows individual counts and total
# Count all text files
wc *.txt
# Recursive count of all files
find . -name "*.txt" -exec wc {} +
Practical Examples
Log file analysis:
# Count log entries
wc -l /var/log/syslog
# Count error entries
grep "ERROR" /var/log/application.log | wc -l
# Count unique IP addresses in access log
awk '{print $1}' /var/log/apache2/access.log | sort -u | wc -l
Code analysis:
# Count lines of code in Python files
find . -name "*.py" -exec wc -l {} + | tail -1
# Count total words in documentation
wc -w *.md | tail -1
# Count non-empty lines
grep -v '^$' filename.txt | wc -l
Data processing:
# Count records in CSV file wc -l data.csv # Count fields in first line (header) head -1 data.csv | tr ',' '\n' | wc -l # Count unique values in a column cut -d',' -f2 data.csv | sort -u | wc -l
Combining with Other Commands
Pipeline usage:
# Count processes ps aux | wc -l # Count files in directory ls -1 | wc -l # Count users logged in who | wc -l # Count network connections netstat -tuln | wc -l
Text processing:
# Count words in specific pattern grep "pattern" file.txt | wc -w # Count lines containing specific text grep -c "search_term" file.txt # Count blank lines grep -c '^$' file.txt
Output Format
Understanding wc output:
# Default output format $ wc example.txt 25 150 800 example.txt ^ ^ ^ ^ | | | filename | | byte count | word count line count # Multiple files with totals $ wc file1.txt file2.txt 10 50 250 file1.txt 15 75 350 file2.txt 25 125 600 total
Special Cases
Empty files and edge cases:
# Empty file wc empty.txt # Output: 0 0 0 empty.txt # File with only newlines wc newlines.txt # Output: 5 0 5 newlines.txt (5 lines, 0 words, 5 bytes) # Binary file wc binary_file # Counts bytes, but word/line counts may not be meaningful
Scripting with wc
Using wc in shell scripts:
#!/bin/bash
# Store line count in variable
line_count=$(wc -l < filename.txt)
echo "File has $line_count lines"
# Check if file has content
if [ $(wc -l < file.txt) -gt 0 ]; then
echo "File is not empty"
fi
# Compare file sizes
file1_lines=$(wc -l < file1.txt)
file2_lines=$(wc -l < file2.txt)
if [ $file1_lines -gt $file2_lines ]; then
echo "file1.txt is larger"
fi
Performance Considerations
Efficient counting:
# Fast line counting (doesn't load entire file) wc -l < largefile.txt # Count only what you need wc -l file.txt # Faster than wc file.txt if you only need lines # For very large files, consider alternatives # Use grep -c for pattern counting grep -c "pattern" hugefile.txt
Common Use Cases
- Log analysis: Count log entries, errors, or specific events
- Code metrics: Count lines of code, comments, or functions
- Data validation: Verify expected number of records in files
- Text analysis: Analyze document length and complexity
- System monitoring: Count processes, connections, or resources
- File processing: Validate file contents before processing
- Backup verification: Compare file counts between directories
- Report generation: Generate statistics for reports
Troubleshooting
Common issues:
# Different results between -c and -m # This happens with multibyte characters (UTF-8) wc -c utf8file.txt # Byte count wc -m utf8file.txt # Character count # No newline at end of file # wc -l might show one less line than expected echo -n "line without newline" | wc -l # Shows 0 # Large files # For very large files, wc might take time # Consider using alternatives for specific patterns
Related Commands and Alternatives
Similar functionality:
# Count specific patterns
grep -c "pattern" file.txt
# Count unique lines
sort file.txt | uniq | wc -l
# Count files in directory
find /path -type f | wc -l
# Count with awk
awk 'END {print NR}' file.txt # Line count
awk '{words += NF} END {print words}' file.txt # Word count