wc Study Notes

wc (word count)

but it can count more than words. It reports statistics about text files or standard input.

If no file is provided, wc reads from standard input.

wc file.txt

By default, wc prints:

  • newline count
  • word count
  • byte count

The order is:

lines words bytes

-l, --lines

Print only the newline character counts.

wc -l file.txt

Example:

12 file.txt

If a file ends in a non-newline character, its trailing partial line is not counted.


-w, --words

Print only the word counts.

A word is a nonempty sequence of non white space delimited by white space characters or by start or end of input. The current locale determines which characters are white space. GNU wc treats encoding errors as non white space.


-c

Print only the byte counts.

  • In UTF-8, one character may occupy multiple bytes, so byte count and character count may differ.

-m, --chars

Print only the character counts, as per the current locale. Encoding errors are not counted.


-L, --max-line-length

Print only the maximum display widths. Tabs are set at every 8th column. Display widths of wide characters are considered. Non-printable characters are given 0 width.

Example output:

78 file.txt

meaning the longest line is 78 columns wide.


Multiple Files

wc a.txt b.txt

Output:

10  50 300 a.txt
20 100 700 b.txt
30 150 1000 total

The final total line is the cumulative count.


--total Option

Controls total-line behavior.

auto (default)

Show total only if multiple files exist.

always

Always print total.

never

Never print total.

only

Print only totals.

wc --total=only a.txt b.txt