Bash wc Command - Count Lines, Words and Characters
Using the wc Command
The wc (word count) command counts lines, words and characters (bytes) in a file.
All examples below use this file, notes.txt:
Bash is a shell.
It runs commands.
It runs scripts too.
Basic Usage
Without options, wc prints three numbers: lines, words and bytes, followed by the file name:
Count Lines (-l)
Use -l to count only the lines:
Count Words (-w)
Use -w to count only the words. Words are separated by spaces or new lines:
Count Characters (-c and -m)
Use -c to count bytes. Use -m to count characters. The result is different only when the text has special characters, like é or emoji, that use more than one byte:
Count Several Files
Give wc more than one file, and it counts each file and adds a total:
Example: Count Lines in Two Files
wc -l notes.txt names.txt
3 notes.txt
3 names.txt
6 total
Use wc with a Pipe
wc is very often used at the end of a pipe, to count results from another command.
When wc reads from a pipe, it prints just the number:
Options Overview
-l: Count lines-w: Count words-c: Count bytes-m: Count characters