wc — Count Lines, Words and Bytes

Practical guide to wc: count lines, words, characters and bytes in files or pipes — from a quick wc -l to the longest line.

wc (word count) counts lines, words, characters and bytes – whether in files or in a pipe. Most often you reach for wc -l to count lines, for example to tally log entries, matches or files. Pass several files and wc appends a total line automatically. Worth knowing: -c counts bytes while -m counts characters (so with UTF-8 an umlaut is correctly one character), and using < file instead of an argument suppresses the filename in the output. This guide shows you the most useful variants for everyday work.

Basic Usage

wc <file> — Show line count, word count, and byte count of a file.

wc README.md

wc <file1> <file2> — Show counts for multiple files with a total summary line.

wc src/*.js

<command> | wc — Count lines, words, and bytes from piped input.

cat access.log | wc

Line Count

wc -l <file> — Count the number of lines in a file.

wc -l access.log

wc -l <file1> <file2> — Count lines in multiple files with a total.

wc -l *.csv

<command> | wc -l — Count the number of lines in command output.

grep 'ERROR' app.log | wc -l

wc -l < <file> — Count lines without printing the filename (redirect input).

wc -l < data.csv

Word Count

wc -w <file> — Count the number of words in a file.

wc -w essay.txt

wc -w <file1> <file2> — Count words in multiple files with a total.

wc -w *.md

<command> | wc -w — Count words in command output.

echo 'hello world foo bar' | wc -w

Character & Byte Count

wc -c <file> — Count the number of bytes in a file.

wc -c image.png

wc -m <file> — Count the number of characters (multibyte-aware, respects locale).

wc -m utf8-text.txt

wc -c < <file> — Get the file size in bytes without the filename.

wc -c < archive.tar.gz

Longest Line

wc -L <file> — Show the length of the longest line in a file.

wc -L config.json

wc -L <file1> <file2> — Show the longest line length for each file with a maximum.

wc -L *.py

<command> | wc -L — Find the length of the longest line in command output.

cat /etc/hosts | wc -L

Combining Flags

wc -lw <file> — Show both line count and word count.

wc -lw README.md

wc -lc <file> — Show line count and byte count.

wc -lc access.log

wc -lwm <file> — Show lines, words, and characters (multibyte-aware).

wc -lwm article.txt

Pipelines & Practical Patterns

find <path> -name '<glob>' | wc -l — Count how many files match a pattern.

find src/ -name '*.js' | wc -l

find <path> -name '<glob>' -exec cat {} + | wc -l — Count total lines of code across all matching files.

find src/ -name '*.py' -exec cat {} + | wc -l

git diff --name-only | wc -l — Count the number of changed files in a git working tree.

git log --oneline | wc -l — Count the total number of commits in a git repository.

ps aux | wc -l — Count the number of running processes (subtract 1 for the header).

ls -1 <path> | wc -l — Count the number of files and directories in a folder.

ls -1 /var/log/ | wc -l

grep -c '' <file> — Alternative line count using grep (equivalent to wc -l).

grep -c '' data.csv

wc -l <files> | sort -rn | head -<n> — Find the largest files by line count.

wc -l src/*.js | sort -rn | head -10

wc -l <files> | tail -1 — Get only the total line count across multiple files.

wc -l *.log | tail -1

awk 'END {print NR}' <file> — Alternative line count using awk (useful in larger awk scripts).

awk 'END {print NR}' access.log

Conclusion

wc is the fastest tool for a sense of size and scope and sits at the end of countless pipelines. Mind the subtle difference between -c (bytes) and -m (characters): for plain ASCII the two are equal, but for UTF-8 text with umlauts or emoji they diverge. Strictly speaking wc -l counts newline characters – if a file ends without a trailing newline, the last line is not counted. And if you use input redirection wc -l < file instead of wc -l file, you get just the bare number without a filename, which is handy to reuse in scripts.

Further Reading

  • sort – sort lines
  • uniq – filter and count duplicate lines
  • grep – search lines by pattern (also counts with -c)