Count Lines, Words, and Bytes in Linux Files with wc
Learn how to use the Linux wc command to count lines, words, and bytes in one or more files, including counts from standard input.
The Linux wc command reports counts from files or standard input. Its name means “word count,” but it can count more than words: by default, it displays newline-based lines, whitespace-delimited words, and bytes.
What the wc command counts
wc is a Linux utility for measuring input. Its common counts are:
- Line count: the number of newline characters. This is what
wc -lreports. - Word count: the number of whitespace-delimited words. This is what
wc -wreports. - Byte count: the number of bytes in the input. This is what
wc -creports.
A newline character is the line-ending character that separates text lines. Because wc counts newline characters rather than visual rows in a text editor, a final visible line without a newline is not included in the line count.
Using wc with one file
Pass a filename after the command:
wc bobs_file.txt
With no counting option, the output fields appear in this order:
| Position | Meaning |
|---|---|
| First | Number of lines, counted as newline characters |
| Second | Number of words |
| Third | Number of bytes |
| Final field | The filename, when a file argument is used |
For example, output shaped like 12 48 317 bobs_file.txt means that the file contains 12 newline characters, 48 words, and 317 bytes. The exact values depend on the file.
Counting several files
wc accepts multiple filenames in one command:
wc file1.txt file2.txt file3.txt
The command prints one result row for each file. Because more than one file was supplied, it also prints a final combined row, usually labeled total. The total adds the corresponding line, word, and byte counts from all supplied files.
wc -l file1.txt file2.txt
With -l, each file receives a line-count row and the final row contains the combined line count. Remember to account for that total row when reading or processing the output.
Selecting one type of count
| Option | What it displays | Example |
|---|---|---|
-l | Newline-based line count | wc -l bobs_file.txt |
-w | Whitespace-delimited word count | wc -w bobs_file.txt |
-c | Byte count | wc -c bobs_file.txt |
| No option | Lines, words, and bytes together | wc bobs_file.txt |
Count lines with -l
wc -l bobs_file.txt
This displays only the number of newline characters, followed by the filename. Use it when the newline-based line total is the only value needed.
Count words with -w
wc -w bobs_file.txt
This suppresses the line and byte columns and displays only the word count and filename.
Count bytes with -c
wc -c bobs_file.txt
This displays the size of the input in bytes and the filename.
Counting command output through standard input
Standard input is data supplied to a command through a pipe or interactive input instead of through a named file. A pipe, written as |, sends one command’s output to another command.
printf 'one\ntwo\nthree\n' | wc -l
The printf command produces three newline-terminated lines, and wc -l counts those three newline characters. When input comes from a pipe, there is no filename field in the result.
Interpreting wc counts correctly
Lines are newline characters, not necessarily visible rows
Consider a file containing two visible text rows where only the first row ends with a newline. An editor may show both rows, but wc -l reports one because the file contains only one newline character.
If the file format is intended to contain a newline at the end, add a final newline with an appropriate editor or file-processing command. The important rule is that wc -l counts newline characters, not lines inferred from display behavior.
Bytes are not always characters
A byte is a unit of stored data. In encodings such as UTF-8, some displayed characters use multiple bytes. Therefore, wc -c can report a larger number than the number of displayed characters.
Treat -c as a byte measurement. If the requirement is to count characters rather than bytes, use character-counting behavior separately and consider the active locale and encoding.
Troubleshooting wc
- The visible row count is larger than
wc -l: the final visible row may not end with a newline character. Inspect the file and add a final newline if the format requires one. - wc reports an error for a filename: verify the path and filename, inspect the directory listing, and check whether the current user has permission to read the file.
- The byte count differs from the expected character count: some characters use multiple bytes in the active encoding. Use
-conly when a byte measurement is intended. - An unexpected total appears: more than one file was supplied. Use one filename for one result, or account for the combined
totalrow.
Quick reference
wc FILE
wc -l FILE
wc -w FILE
wc -c FILE
wc FILE1 FILE2
COMMAND | wc -l
For related command-line foundations, see Linux command-line topics, Bash, and showing the full path of shell commands.