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.mdwc <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 | wcLine Count
wc -l <file> — Count the number of lines in a file.
wc -l access.logwc -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 -lwc -l < <file> — Count lines without printing the filename (redirect input).
wc -l < data.csvWord Count
wc -w <file> — Count the number of words in a file.
wc -w essay.txtwc -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 -wCharacter & Byte Count
wc -c <file> — Count the number of bytes in a file.
wc -c image.pngwc -m <file> — Count the number of characters (multibyte-aware, respects locale).
wc -m utf8-text.txtwc -c < <file> — Get the file size in bytes without the filename.
wc -c < archive.tar.gzLongest Line
wc -L <file> — Show the length of the longest line in a file.
wc -L config.jsonwc -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 -LCombining Flags
wc -lw <file> — Show both line count and word count.
wc -lw README.mdwc -lc <file> — Show line count and byte count.
wc -lc access.logwc -lwm <file> — Show lines, words, and characters (multibyte-aware).
wc -lwm article.txtPipelines & Practical Patterns
find <path> -name '<glob>' | wc -l — Count how many files match a pattern.
find src/ -name '*.js' | wc -lfind <path> -name '<glob>' -exec cat {} + | wc -l — Count total lines of code across all matching files.
find src/ -name '*.py' -exec cat {} + | wc -lgit 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 -lgrep -c '' <file> — Alternative line count using grep (equivalent to wc -l).
grep -c '' data.csvwc -l <files> | sort -rn | head -<n> — Find the largest files by line count.
wc -l src/*.js | sort -rn | head -10wc -l <files> | tail -1 — Get only the total line count across multiple files.
wc -l *.log | tail -1awk '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
- GNU Coreutils manual: wc – complete reference for every option
- man7.org: wc(1) – the Linux manual page