wc Command in Linux: Count Lines, Words, and Bytes

On Linux and Unix-like operating systems, the wc command counts the number of lines, words, characters, and bytes in each given file or standard input and prints the result.
This guide explains how to use the wc command through practical examples.
wc Command Syntax
The syntax for the wc command is as follows:
wc [OPTIONS] [FILE]...wc accepts zero or more input file names. If no file is specified, or when FILE is -, wc reads from standard input. A word is defined as a non-empty string of characters delimited by whitespace.
In its simplest form, used without any options, wc prints three columns: the number of lines, words, and bytes, followed by the file name. When reading from standard input, the file name column is omitted.
For example, to display information about /proc/cpuinfo:
wc /proc/cpuinfo448 3632 22226 /proc/cpuinfo- 448 — number of lines
- 3632 — number of words
- 22226 — number of bytes
When reading from standard input, the file name is not shown:
wc < /proc/cpuinfo448 3632 22226To process more than one file at a time, pass multiple file names separated by spaces. wc prints a result for each file and a total line at the end:
wc /proc/cpuinfo /proc/meminfo448 3632 22226 /proc/cpuinfo
49 143 1363 /proc/meminfo
497 3775 23589 totalOptions
The following options control which counts are printed:
-l,--lines— Print the number of lines.-w,--words— Print the number of words.-m,--chars— Print the number of characters (may differ from bytes for multibyte UTF-8 text).-c,--bytes— Print the number of bytes.-L,--max-line-length— Print the length of the longest line.
When multiple options are combined, counts are printed in this order: lines, words, characters, bytes, maximum line length.
To display only the word count:
wc -w /proc/cpuinfo3632 /proc/cpuinfoTo print the line count and the length of the longest line:
wc -lL /proc/cpuinfo448 792 /proc/cpuinfoCharacters vs Bytes
The -m and -c options are not interchangeable. For ASCII text, they return the same value because each character is one byte. For files containing multibyte UTF-8 characters such as accented letters or emoji, the byte count (-c) will be higher than the character count (-m).
To count characters in a UTF-8 encoded file:
wc -m file.txtTo count bytes in the same file:
wc -c file.txtThe --files0-from=F option reads input from files whose NUL-terminated names are listed in file F. This is useful when combining wc with the find command
:
find /etc -name 'host*' -print0 | wc -l --files0-from=- 4 /etc/host.conf
27 /etc/avahi/hosts
1 /etc/hostname
14 /etc/hosts
46 totalCount the Number of Lines
The -l option is the most common use of wc. To count the number of lines in the /etc/passwd
file:
wc -l /etc/passwd44 /etc/passwdCount the Number of Words
To count the number of words in a file, use wc -w followed by the file name:
wc -w ~/Documents/file.txt512 /home/linuxize/Documents/file.txtwc in Pipelines
wc is frequently used in combination with other commands through piping.
Count Files in the Current Directory
The following command counts the number of regular files in the current directory:
find . -type f | wc -lCount Matching Lines with grep
To count how many lines in a file match a pattern, pipe grep
output to wc -l:
grep "error" /var/log/syslog | wc -lThis is a common pattern for log analysis and can be combined with sort
for further processing. If you only need the count, grep -c "error" /var/log/syslog is a direct alternative.
Count the Number of Users
The following command counts the number of user accounts on the system by counting the lines in the output of getent passwd:
getent passwd | wc -lTroubleshooting
wc -l returns a count that is one less than expected
wc -l counts newline characters. If the last line of the file does not end with a newline, it is not counted. Add a trailing newline to the file or use grep -c '' as an alternative that counts all lines regardless.
-c and -m return different values
The file contains multibyte UTF-8 characters. -c counts raw bytes and -m counts Unicode characters. For plain ASCII text they are identical; for text with accented letters, emoji, or non-Latin scripts, they will differ.
wc output includes leading spaces
wc right-aligns numbers in columns, which adds leading spaces when processing multiple files. To extract just the number, use wc -l < file.txt (redirect rather than passing the file name) so the file name and padding are omitted.
Quick Reference
| Command | Description |
|---|---|
wc file.txt | Print lines, words, and bytes |
wc -l file.txt | Count lines only |
wc -w file.txt | Count words only |
wc -c file.txt | Count bytes only |
wc -m file.txt | Count characters (UTF-8 aware) |
wc -L file.txt | Print length of the longest line |
wc file1 file2 | Count across multiple files with total |
command | wc -l | Count lines of command output |
For a printable quick reference, see the wc cheatsheet .
FAQ
What is the difference between -c and -m?
-c counts raw bytes in the file. -m counts characters, interpreting the encoding correctly. For ASCII-only text both values are the same. For UTF-8 text with multibyte characters, -c returns a higher value than -m.
Why does wc -l return 0 for a file I know has content?
The file may have content on one line with no trailing newline character. wc -l only counts \n characters. Use wc -l < file.txt after verifying the file has a newline at the end, or use grep -c '' file.txt to count all lines regardless.
How do I count words across multiple files and get only the total?
Pass all file names to wc -w and look at the last line of the output, which shows the total. To print only the total number, use: wc -w file1 file2 | tail -1.
Conclusion
The wc command counts lines, words, bytes, and characters in one or more files or from standard input. It works especially well in pipelines alongside commands like grep, find, and sort.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page