wc

How many lines in this file, how many errors in this log, how many files in this folder? wc -l answers.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • wc app.log
  • wc -l users.csv
  • grep ERROR app.log | wc -l
  • ls /etc | wc -l
  • wc -w app.log

How it works

wc FILE prints three counts: lines, words and bytes (characters), then the name. Most of the time you want just one, so add -l, -w or -c.

Its real power is at the end of a pipe: grep ERROR app.log | wc -l counts the error lines, ls | wc -l counts what is in a folder.

Syntax

wc [-l] [-w] [-c] [FILE]...

Options

OptionWhat it does
-lcount lines
-wcount words
-ccount bytes (characters, for plain text)

Good to know

  • A CSV with a header row has one data row fewer than wc -l says.
  • grep -c ERROR app.log counts matching lines too, without the pipe.