wc Command on Linux: Count Lines, Words and Bytes in Files

wc command linux - custom-wc-featured.png

wc on Linux is the fastest sanity check — “how many lines in this log?”, “did my export have the expected row count?”, “is this file empty?” Scripts often store lines=$(wc -l < file) for loops and validation.

Pair with: split,
grep,
tee.

Default output

Shows lines, words, and bytes unless you pass flags only.

bashall counts
wc /var/log/syslog
wc
file

Lines only

-l is the most used flag for scripts and pipelines.

bashlines
wc -l access.log
grep ERROR app.log | wc -l
wc -l
lines

Bytes and chars

-c bytes; -m characters (locale-aware Unicode).

bashbytes
wc -c image.bin
wc -c
size

Pipes and variables

Reading from stdin avoids filename in the count line.

TipUse wc -l < file so the filename does not appear in the output (clean integer in scripts).
stdin
variable

Quick Reference

  • Lines: wc -l file
  • Pipe: cmd | wc -l
  • Script: lines=$(wc -l < file)

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.