ZetCode

Linux wc Command

last modified October 23, 2026

The wc (word count) command in Linux counts lines, words, characters, and bytes in files or in standard input. It is one of the most frequently used tools at the end of a pipeline, answering questions such as how many files are in a directory, how many lines a log has, or how many results a search returned.

A word is a sequence of non-whitespace characters, and a line is counted for each newline character. The examples use GNU wc from coreutils.

Basic usage

Without options, wc prints the number of lines, words, and bytes, followed by the file name.

wc thermopylae.txt

The file has 3 lines, 38 words, and 223 bytes. The counts are always printed in this order.

$ wc thermopylae.txt
  3  38 223 thermopylae.txt

Selecting counts

The -l, -w, and -c options print only the lines, words, or bytes. They can be combined.

wc -l thermopylae.txt
wc -w thermopylae.txt
wc -c thermopylae.txt
wc -lw thermopylae.txt

The order of the options does not matter; the output always follows the order lines, words, characters, bytes.

$ wc -l thermopylae.txt
3 thermopylae.txt
$ wc -w thermopylae.txt
38 thermopylae.txt
$ wc -c thermopylae.txt
223 thermopylae.txt
$ wc -lw thermopylae.txt
  3  38 thermopylae.txt

Characters versus bytes

The -c option counts bytes, while -m counts characters according to the current locale.

wc -c slovak.txt
wc -m slovak.txt

The text Žltý kôň with its newline has 9 characters, but in UTF-8 the letters Ž, ý, ô, and ň take two bytes each, which gives 13 bytes. For pure ASCII text, both counts are the same.

$ wc -c slovak.txt
13 slovak.txt
$ wc -m slovak.txt
9 slovak.txt

Multiple files

When given several files, wc prints a line for each of them and a total.

wc -l thermopylae.txt words.txt

Shell wildcards make it easy to count lines in many files, for example wc -l *.txt.

$ wc -l thermopylae.txt words.txt
  3 thermopylae.txt
  7 words.txt
 10 total

Reading standard input

When reading standard input, wc prints no file name, which is handy for using the number in scripts.

wc -l < words.txt
lines=$(wc -l < words.txt)
echo "The file has $lines lines"

Redirecting the file with < instead of passing its name yields just the number, so we can store it in a variable without further processing.

$ wc -l < words.txt
7
$ lines=$(wc -l < words.txt)
$ echo "The file has $lines lines"
The file has 7 lines

Longest line

The -L option prints the length of the longest line.

wc -L thermopylae.txt

The longest line has 78 characters. This is useful for checking line length limits in code or text files. Tabs are counted as advancing to the next tab stop.

$ wc -L thermopylae.txt
78 thermopylae.txt

Counting in pipelines

Most often, wc -l counts the lines produced by another command.

ls src | wc -l
find src -type f | wc -l
grep -c "int" src/main.c
sort -u words.txt | wc -l

The first command counts the entries in the src directory, while find counts all files in the tree. To count matching lines, the -c option of grep does the job without wc. The last pipeline counts unique words. Counting the output of ls is fine for a quick check, but file names with newlines would be counted more than once.

$ ls src | wc -l
2
$ find src -type f | wc -l
3
$ grep -c "int" src/main.c
1
$ sort -u words.txt | wc -l
6

The last line without a newline

wc -l counts newline characters, not lines in the visual sense.

printf "one\ntwo" | wc -l
printf "one\ntwo\n" | wc -l

If the last line of a file does not end with a newline, it is not counted. Text files on Unix systems should end with a newline; many editors add it automatically.

$ printf "one\ntwo" | wc -l
1
$ printf "one\ntwo\n" | wc -l
2

Counting lines in many files

The --files0-from option reads a list of NUL-separated file names, which works well with find -print0.

find src -name "*.[ch]" -print0 | wc -l --files0-from=-

We count the lines of all C source and header files in a directory tree. The - argument means the list comes from standard input. Unlike passing the names as arguments, this handles any number of files and any file names safely.

$ find src -name "*.[ch]" -print0 | wc -l --files0-from=-
5 src/lib/util.c
1 src/lib/util.h
3 src/main.c
9 total

Best practices for wc

Source

GNU wc manual

In this article, we have used the wc command to count lines, words, characters, and bytes in files and pipelines.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Linux tutorials.