Where to start First steps track · 15 stages
Linux Labs

wc — count lines, words and bytes

4 min Ubuntu 24.04 · GNU coreutils 9.4

The wc command counts lines (-l), words (-w), bytes (-c) and characters (-m) in a file or in command output.

Cheat sheet

7 commands Printable version
  • wc file.txt lines, words, bytes and the name
  • wc -l file.txt lines only: "2 file.txt"
  • wc -l < file.txt just the number: "2"
  • grep error app.log | wc -l how many lines contain error
  • wc -l *.log per file, plus a total
  • find /etc -name '*.conf' | wc -l how many files were found
  • wc -m text.txt characters, not bytes

Overview

wc (word count) prints three numbers: lines, words and bytes. A word is a run of text between spaces, tabs and newlines. -l, -w and -c keep only the number you need:

text
student@ubuntu:~$ printf 'one two\nthree\n' > words.txt
student@ubuntu:~$ wc words.txt
 2  3 14 words.txt
student@ubuntu:~$ wc -l words.txt
2 words.txt
student@ubuntu:~$ wc -l < words.txt
2

wc counts a line for every newline character. When the file name is an argument, wc prints it next to the number. When the text arrives on standard input — through < or a pipeline — there is no name, just the number, which is handy in scripts and comparisons.

Options

5

Options combine: wc -lw prints lines and words. The full list is in man wc.

Option What it does Example
-l --lines Count lines (newline characters). wc -l app.log
-w --words Count words separated by whitespace. wc -w notes.txt
-c --bytes Count bytes. wc -c image.png
-m --chars Count characters by locale; in UTF-8 a Cyrillic letter is one character. wc -m text.txt
-L --max-line-length Width of the longest line. wc -L config.yaml

Nothing found. Try a shorter query.

Recipes

5

How many lines contain an error

bash
grep -i error /var/log/app.log | wc -l
grep -ci error /var/log/app.log      # the same in one command

How many files are in a directory

bash
find /var/log -maxdepth 1 -type f | wc -l

ls | wc -l counts too, but skips hidden files, and ls -l | wc -l adds the total line.

How many distinct values

bash
cut -d: -f7 /etc/passwd | sort -u | wc -l    # number of distinct shells

Several files at once

bash
wc -l /etc/passwd /etc/group      # a line per file and a total line

The longest line

bash
wc -L config.yaml     # width of the longest line

Troubleshooting

5

wc -l shows one line fewer

wc counts newlines. If the last line of a file has no \n, it is not counted: printf 'a\nb' | wc -l prints 1.

There is an extra file name in the output

The name is printed when the file is an argument. Feed the file on standard input instead: wc -l < file.txt.

wc -c and wc -m give different numbers

-c counts bytes, -m counts characters according to the locale. In UTF-8 a Cyrillic letter is one character and two bytes: for привет plus a newline, -c gives 13 and -m gives 7.

ls | wc -l gives the wrong number of files

ls without -A skips hidden files, and ls -l adds a total line. Count with find … | wc -l.

wc: x: Is a directory

wc reads files, not directories. To count the files in a directory use find: find x -type f | wc -l.

See also

  • grep — pick lines before counting; grep -c counts on its own.
  • sort and uniq — remove repeats to count distinct values.
  • find — find the files you then count.
  • redirection — input from a file with < and the | pipeline.
  • text-tools — tr, paste, column and other pipeline links.
Loading…