Skip to main content

wc Cheatsheet

By Dejan Panovski Updated on Download PDF

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.

CommandDescription
wc file.txtShow lines, words, and bytes for a file
wc file1 file2Show counts per file and a total line
wc *.logCount all matching files
command | wcCount output from another command
wc --helpShow available options

Common Count Flags

Use flags to request specific counters.

CommandDescription
wc -l file.txtCount lines only
wc -w file.txtCount words only
wc -c file.txtCount bytes only
wc -m file.txtCount characters only
wc -L file.txtShow longest line length

Useful Pipelines

Practical wc combinations for quick shell checks.

CommandDescription
ls -1 | wc -lCount directory entries (one per line)
grep -r "ERROR" /var/log | wc -lCount matching log lines
find . -type f | wc -lCount files recursively
ps aux | wc -lCount process list lines
cat file.txt | wc -wCount words from stdin

Multi-File Counting

Summarize multiple files and totals.

CommandDescription
wc -l *.txtLine count for each file plus total
wc -w docs/*.mdWord count per Markdown file and total
wc -c part1 part2 part3Byte count per file and combined total
wc -m *.csvCharacter count for each CSV file
wc -L *.logMax line length per file and max total

Script-Friendly Patterns

Extract numeric output safely in scripts.

CommandDescription
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 ... fiThreshold check in scripts
printf '%s\n' "$text" | wc -mCount characters in a variable

Troubleshooting

Quick checks for common wc confusion.

IssueCheck
Count includes filenameUse input redirection: wc -l < file
Unexpected word countConfirm whitespace and delimiters in the file
Character and byte counts differUse -m for characters and -c for bytes
Total line missing with many filesEnsure shell glob matches at least one file
Pipeline count seems off by oneSome commands add headers; account for that

Use these guides for complete text-processing workflows.

GuideDescription
linux wc CommandFull wc guide with detailed examples
head Command in LinuxShow first lines of files
tail Command in LinuxShow last lines and follow logs
grep Command in LinuxSearch and filter matching lines
find Files in LinuxBuild file lists for counting