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

By 

Updated on

9 min read

Using the Linux wc command to count lines, words, and bytes

When you need to measure a text file, compare source files, or count command output, checking the contents by hand is impractical. The wc command reports the number of lines, words, characters, and bytes in files or standard input.

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 according to the current locale (may differ from bytes in a multibyte locale such as UTF-8).
  • -c, --bytes - Print the number of bytes.
  • -L, --max-line-length - Print the display width of the longest line. Tabs and wide characters occupy more than one column, so this is not always the same as the number of characters.

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. The -m option interprets input according to the current locale, while -c counts raw bytes. In a UTF-8 locale, both options return the same value for ASCII text because each character is one byte. For text containing multibyte characters such as accented letters or emoji, the byte count will be higher than the character count.

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

Controlling the Total Line

When you pass more than one file, wc ends the output with a total line. The --total=WHEN option takes control of that line, where WHEN is auto (the default), always, only, or never.

Reach for --total=only when the per-file breakdown is noise and you want the combined figure on its own:

Terminal
wc -l --total=only src/*.py
output
483

Only one count is printed here, so wc omits the usual column padding and the value is safe to assign straight to a variable.

--total=never does the opposite and drops the summary line. That is the form to use when the output feeds another command, since the total would otherwise arrive as one more row of data.

This option landed in GNU coreutils 9.2, so Ubuntu 24.04 and Debian 13 have it, while Ubuntu 22.04 and RHEL 9 still ship coreutils 8.32 without it.

Reading File Names From a List

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

Count Characters in a String

wc reads standard input, so you can measure a string without writing it to a file first. The trap is that echo appends a newline, and wc -c counts that newline like any other byte:

Terminal
echo "hello" | wc -c
output
6

The string is five characters long, but the count comes back as six. Either suppress the newline with echo -n, or use printf with an explicit format string, which never adds one:

Terminal
printf '%s' "hello" | wc -c
output
5

Of the two, printf is the safer choice, because echo -n behaves differently depending on which shell runs it.

The same approach works on a shell variable. In a UTF-8 locale, this is where -c and -m part ways as soon as the text leaves ASCII:

Terminal
text="héllo wörld"
printf '%s' "$text" | wc -m
output
11
Terminal
printf '%s' "$text" | wc -c
output
13

Both accented letters take two bytes in UTF-8, so the byte count runs two ahead of the character count. Bash also uses the current locale when evaluating string length. With a UTF-8 locale, it returns the same character count with no pipeline and no subprocess:

Terminal
echo "${#text}"
output
11

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 . -maxdepth 1 -type f -printf '\n' | wc -l

The -printf '\n' action emits one newline per matched file instead of printing file names, so names containing newlines do not inflate the count. Drop -maxdepth 1 to count every regular file in the directory tree instead. Unlike ls, find also includes hidden regular files. The guide on counting files in a directory compares the alternatives in more detail.

Count Lines Across Multiple Files

A shell glob covers the files in one directory, but source trees are usually nested. Pass a NUL-terminated list from find directly to wc to count every matching file at any depth:

Terminal
find . -type f -name '*.py' -print0 | wc -l --files0-from=-
output
  120 ./src/util.py
   45 ./src/cli.py
  318 ./src/app.py
  483 total

Every file gets its own row, and the total sits at the bottom. NUL separators safely handle spaces, quotes, and newlines in file names. Passing the list through --files0-from=- also keeps it in one wc invocation, so a large file set does not produce multiple total lines.

Sorting that output ranks the files by size, which is a quick way to find the longest ones. The catch is that the total is the largest number in the list, so it lands at the top and pushes a real file off the end of the results. Remove it with --total=never:

Terminal
find . -type f -name '*.py' -print0 |
  wc -l --total=never --files0-from=- |
  sort -rn |
  head -n 3
output
  318 src/app.py
  120 src/util.py
   45 src/cli.py

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 current locale recognizes multibyte characters in the file. The -c option counts raw bytes, while -m counts characters according to that locale. Check the output of locale if -m does not match the file’s encoding.

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

For a printable quick reference, see the wc cheatsheet .

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 according to the current locale
wc -L file.txtPrint length of the longest line
wc file1 file2Count across multiple files with total
command | wc -lCount lines of command output
wc -l < file.txtCount lines and print the number on its own
wc -l --total=only *.pyPrint only the combined total
printf '%s' "$var" | wc -mCount the characters in a variable

FAQ

What is the difference between -c and -m?
-c counts raw bytes in the file. -m counts characters according to the current locale and does not count encoding errors. In a UTF-8 locale, both values are the same for ASCII text, while -c returns a higher value for multibyte characters.

Why does wc -c report one more byte than my string has characters?
echo appends a trailing newline and wc counts it. Measure the string on its own with printf '%s' "text" | wc -c, or use echo -n when you know which shell will run the command.

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?
With GNU coreutils 9.2 or newer, use wc -w --total=only file1 file2. On older versions, extract the count from the final row with wc -w file1 file2 | awk 'END { print $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.

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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page