Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux wc command

Reference for the Linux wc command: count lines, words, characters and bytes in files or pipelines, find the longest line, and avoid common counting mistakes.

wc ("word count") counts the lines, words and bytes of its input. With no options it prints all three numbers followed by the file name. With one option it prints just that count, which makes it the usual last stage of a pipeline: how many files matched, how many lines were produced.

A line is counted by its newline character, a word is a run of non-whitespace characters, and a byte is a raw byte.

Terminal
wc [OPTION]... [FILE]...

Try it

Bash The three counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Totals, characters and the missing newline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
wc [-clmw] [-L] [FILE]...
wc --files0-from=F
Option Description
-l, --lines Count newline characters.
-w, --words Count words.
-c, --bytes Count bytes.
-m, --chars Count characters according to the current locale.
-L, --max-line-length Print the length of the longest line.
--files0-from=F Read NUL-separated file names from F (- for standard input).

Counts always print in the fixed order lines, words, characters, bytes, longest line, whatever order you type the options.

Returns: 0 on success; non-zero if a file cannot be read. With more than one file, a final total line is added.

Usage notes

  • Redirect to hide the name. wc -l file prints 5 file; wc -l < file prints only 5, which is easier to use in scripts.
  • Lines are newlines. A final line without a trailing newline is not counted by -l, as the example above shows.
  • Number formatting. The width of the number columns can vary between implementations and inputs, so for arithmetic use wc -l < file inside $(( )) or read, not text slicing.
  • Characters need a locale. In the C locale -m equals -c. Under a UTF-8 locale multi-byte characters count once.
  • Counting matches. grep -c counts matching lines directly; grep -o pattern | wc -l counts every occurrence, even several on one line.
  • Huge lists of files: find . -type f -print0 | wc --files0-from=- avoids trouble with spaces in names, and the final total line is the sum.
  • Words are whitespace-delimited, so foo-bar is one word and punctuation sticks to words. It is not a linguistic word counter.
  • Empty input gives zeros, not an error.