Skip to content

wc

Counts lines, words and bytes in files or in the output of other commands.

wc output

Syntax

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

Options

Option Description
-l, --lines Count lines
-w, --words Count words
-c, --bytes Count bytes
-m, --chars Count characters (differs from bytes for umlauts and other UTF-8 characters)
-L, --max-line-length Show the length of the longest line

Examples

Count lines, words and bytes:

wc /var/log/nginx/access.log
  9 108 850 /var/log/nginx/access.log

The three numbers are lines, words and bytes.

Only count lines:

wc -l /var/log/nginx/access.log
9 /var/log/nginx/access.log

Count several files at once:

wc -l /var/log/nginx/access.log users.csv
  9 /var/log/nginx/access.log
  4 users.csv
 13 total

Count the results of another command:

grep ' 404 ' /var/log/nginx/access.log | wc -l
3

Only print the number, without the file name:

wc -l < users.csv
4

When do I need it?

  • To find out how big a log is or how many requests a server got
  • To count matches together with grep, for example errors, logins or 404 requests
  • To count files, users or processes: ls /etc/nginx/sites-enabled | wc -l
  • In scripts that need a number to compare, for example "more than 100 errors"

Number only for scripts

With wc -l < FILE, wc reads from standard input and prints only the number. That is easier to use in scripts than the output with the file name.

The last line needs a line break

wc -l counts line breaks, not lines. If the last line of a file has no line break at the end, it is not counted.

cat · sort · grep