opscape

Viewing files

wc

Count lines, words, characters and bytes in files or standard input.

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

11 of 11 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: wc(1)

wc (word count) prints, for each file, the number of newline characters, words and bytes, in that order, followed by the file name. Options pick which counts you want: -l lines, -w words, -c bytes, -m characters, -L the length of the longest line.

By far the most common use is wc -l at the end of a pipeline, to answer "how many?": how many matches, how many files, how many unique visitors. Two details matter in practice: -l counts newline characters, so a last line without one is not counted; and when you read from standard input (wc -l < file) there is no file name in the output, which is what you want in a script.

Examples

Lines, words and bytes of a file

wc todo.txt
explain
output
 5 12 62 todo.txt

todo.txt has 5 lines, 12 words and 62 bytes. The columns are always in that order.

Count lines

wc -l access.log
explain
output
20 access.log

-l prints only the line count: 20 requests in the log.

Get just the number, for a script

count=$(wc -l < todo.txt)
echo "There are $count items to do"
output
There are 5 items to do

Reading from standard input with < means wc has no file name to print, so the substitution captures only the number.

Several files with a total

wc -l logs/*
explain
output
 12 logs/app.log
  3 logs/app.log.1
  2 logs/error.log
 17 total

Each file gets its own line and wc adds a total line at the end.

Count the results of a pipeline

grep -c . access.log; grep ' 404 ' access.log | wc -l
explain
output
20
4

grep ' 404 ' | wc -l counts the not-found requests: 4. For a single file grep -c gives the same answer without the extra process, as the first count (all 20 non-empty lines) shows.

Count distinct values

cut -d ' ' -f 1 access.log | sort -u | wc -l
explain
output
6

Extract the client IP of every request, keep each one once, and count them, giving 6 distinct clients.

Count words

wc -w data/poem.txt
explain
output
16 data/poem.txt

A word is any run of characters separated by whitespace, so dog. and sleeps; count as one word each.

Characters versus bytes

printf 'café\n' | wc -m -c
explain
output
      5       6

-m counts characters (5: c a f é and the newline) and -c counts bytes (6), because é takes two bytes in UTF-8. The output order is fixed, characters before bytes, whatever order you give the options in.

A last line without a newline is not counted

printf 'one\ntwo\nthree' | wc -l
explain
output
2

There are three lines of text but only two newline characters, and -l counts newlines, so the answer is 2. Files written by some editors and by printf without \n behave like this.

Length of the longest line

wc -L access.log
explain
output
144 access.log

-L reports the display width of the longest line, 144 columns here: useful before choosing a terminal width or a column limit.

Count lines in files found by find

find src -name '*.py' -print0 | wc -l --files0-from=-
explain
output
3 src/lib/helpers.py
17 src/app.py
20 total

--files0-from=- reads NUL-separated file names from standard input, so names with spaces or newlines are safe and there is no limit on how many files find returns.

Common pitfalls

  • wc -l file prints the file name after the number. Use wc -l < file to get only the number instead of piping through awk '{print $1}'.

  • BSD/macOS wc pads the number with leading spaces even when reading standard input, so [ "$(wc -l < f)" = 5 ] fails there. Compare numerically with -eq, or strip spaces with $(( $(wc -l < f) )).

  • wc -l counts newline characters: a file whose last line has no newline reports one line fewer than you see.

  • ls | wc -l miscounts file names that contain newlines; find . -maxdepth 1 -printf . | wc -c counts entries safely.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • --files0-from and --total are GNU options. BSD/macOS wc has -l, -w, -c and -m; for anything else check your system's man page.

  • macOS wc right-aligns numbers in a wide field even for one count read from standard input; GNU prints the bare number.

Options worth knowing

A selection, described in our own words. For the full list, see wc(1) or wc --help.

OptionWhat it does
-l, --linesPrint the number of newline characters.
-w, --wordsPrint the number of whitespace-separated words.
-c, --bytesPrint the number of bytes.
-m, --charsPrint the number of characters, which differs from bytes for multi-byte text.
-L, --max-line-lengthPrint the display width of the longest line.
--files0-from FILERead NUL-terminated file names from FILE (- for standard input).
--total[=WHEN]When to print the total line: auto, always, only or never.

Related