wc command in UNIX


wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F

Introduction

wc command prints newline, word and byte counts for each FILE, and a total line if more than one FILE is specified.

Note that a word is a non-zero-length sequence of characters delimited by white space.

With no FILE, or when FILE is -, wc command reads from standard input.

Options

The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length.

  • -c, --bytes: print the byte counts
  • -m, --chars: print the character counts
  • -l, --lines: print the newline counts
  • --files0-from=F: read input from the files specified by NUL-terminated names in file F
  • -L, --max-line-length: print the maximum display width
  • -w, --words: print the word counts

Examples

wc is often used with pipe operator together with previous commands.

# use it all alone
wc silvia.txt
 18  85 454 silvia.txt

Print the character counts that makes the names of 3 last modified files in $HOME directory:

ls -t $HOME | head -n 3 | wc -c
27 # this number will change very often, off course

Count connected users:

who | wc -l
5

# as counter-check
who
pit      tty1         2022-03-21 13:38 (:0)
pit      pts/0        2022-03-21 13:38 (:0)
pit      pts/2        2022-03-21 16:08 (:0)
pit      pts/3        2022-03-21 16:31 (:0)
pit      pts/4        2022-03-21 16:41 (:0)

Quotes

Here are some useful links I used while writing this article: