Table of Contents

The wc command counts lines, words, bytes, and characters in files or standard input. It is commonly used in data validation, scripting checks, and quick content sizing.

Overview

Use wc when you need to:

  • Count total lines in a file
  • Measure word counts for text content
  • Compare file sizes by bytes/characters
  • Build simple validation checks in pipelines

Syntax

wc [options] [file...]

Common forms:

# Default output: lines, words, bytes
wc notes.txt

# Line count only
wc -l app.log

# Count from pipeline
grep -i error app.log | wc -l

Common Options

Option Purpose Example
-l Count lines wc -l file.txt
-w Count words wc -w file.txt
-c Count bytes wc -c file.txt
-m Count characters wc -m file.txt
-L Print maximum display line length wc -L file.txt

Default Output Columns

Without options, wc prints:

  1. Line count
  2. Word count
  3. Byte count
  4. Filename (when file input is used)

For multiple files, wc also prints a total line.

Examples

# Full count for one file
wc report.txt
# Count lines in all markdown files
wc -l *.md
# Count words in documentation file
wc -w README.md
# Count bytes from command output
cat payload.json | wc -c
# Count matching log lines
grep -i "ERROR" app.log | wc -l
# Show longest line length in file
wc -L data.txt

Pipeline and Script Patterns

Count files matched by find safely:

find . -type f -name '*.log' | wc -l

Validate minimum line count in a script:

line_count=$(wc -l < input.txt)
if [[ "$line_count" -lt 10 ]]; then
	echo "input.txt has too few lines" >&2
	exit 1
fi

Note: using input redirection (wc -l < file) returns just the number, which is easier to parse in scripts.

Safe Usage Guidelines

  1. Use explicit flags (-l, -w, etc.) for clarity.
  2. Prefer < file form in scripts for parse-friendly output.
  3. Be aware that word counting depends on locale and whitespace rules.
  4. For Unicode-heavy content, compare -c and -m when needed.

Troubleshooting

Counts do not match expectations

Possible causes:

  • Trailing newline differences
  • Locale or encoding differences
  • Hidden characters or unusual whitespace

Fixes:

cat -A file.txt
wc -lwmc file.txt

Script parsing fails due to filename in output

Cause: wc includes filename when file argument is used.

Fix:

count=$(wc -l < file.txt)

Notes

wc is simple but effective for quick quantification and pipeline checks. For advanced text analytics, combine with awk, sed, or language-specific tooling.

For command details and implementation-specific behavior, run:

man wc
wc --help