Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Use the wc Command in Linux

Count lines, words, characters and bytes in files and command output.

How-To Guides2 min read5 screenshots

What is wc

wc means "word count". It counts the lines, words and bytes in files or in piped text.

Syntax

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

Options

  • -c, --bytes counts the bytes.
  • -m, --chars counts the characters.
  • -l, --lines counts the lines (new line characters).
  • -w, --words counts the words.
  • -L, --max-line-length prints the length of the longest line.
  • --files0-from=F reads the file names from a file F, separated by NUL characters.
  • --help shows the help page.
  • --version shows the version.

Examples

1. Count a file

wc exampleFile.txt
Using wc on a file

The output shows three numbers and the file name: lines, words and bytes.

  12  86 540 exampleFile.txt

2. Count several files

wc exampleFile.txt employeeNames.txt
Using wc on multiple files

You get one line per file and a total line at the end.

3. Count the lines

wc -l exampleFile.txt
Counting the lines

4. Count the words

wc -w exampleFile.txt
Counting the words

5. Count the characters and bytes

wc -m exampleFile.txt
wc -c exampleFile.txt
Counting the characters and bytes

The numbers differ when the file has multi-byte characters (like accented letters).

Use wc with other commands

Count the files in a folder:

ls | wc -l

Count the logged-in users:

who | wc -l

Count how many times a word appears as a line in a log:

grep -c "error" app.log

Count the lines of all .py files:

cat *.py | wc -l

Find the longest line:

wc -L file.txt

Quick recap

  • wc -l lines, wc -w words, wc -c bytes, wc -m characters.