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

By 

Updated on

5 min read

Using the Linux wc command to 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:

txt
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:

Terminal
wc /proc/cpuinfo
output
448 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:

Terminal
wc < /proc/cpuinfo
output
448 3632 22226

To 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:

Terminal
wc /proc/cpuinfo /proc/meminfo
output
448 3632 22226 /proc/cpuinfo
 49  143  1363 /proc/meminfo
497 3775 23589 total

Options

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:

Terminal
wc -w /proc/cpuinfo
output
3632 /proc/cpuinfo

To print the line count and the length of the longest line:

Terminal
wc -lL /proc/cpuinfo
output
448 792 /proc/cpuinfo

Characters 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:

Terminal
wc -m file.txt

To count bytes in the same file:

Terminal
wc -c file.txt

The --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 :

Terminal
find /etc -name 'host*' -print0 | wc -l --files0-from=-
output
 4 /etc/host.conf
27 /etc/avahi/hosts
 1 /etc/hostname
14 /etc/hosts
46 total

Count 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:

Terminal
wc -l /etc/passwd
output
44 /etc/passwd

Count the Number of Words

To count the number of words in a file, use wc -w followed by the file name:

Terminal
wc -w ~/Documents/file.txt
output
512 /home/linuxize/Documents/file.txt

wc 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:

Terminal
find . -type f | wc -l

Count Matching Lines with grep

To count how many lines in a file match a pattern, pipe grep output to wc -l:

Terminal
grep "error" /var/log/syslog | wc -l

This 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:

Terminal
getent passwd | wc -l

Troubleshooting

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

CommandDescription
wc file.txtPrint lines, words, and bytes
wc -l file.txtCount lines only
wc -w file.txtCount words only
wc -c file.txtCount bytes only
wc -m file.txtCount characters (UTF-8 aware)
wc -L file.txtPrint length of the longest line
wc file1 file2Count across multiple files with total
command | wc -lCount 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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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