The wc command

print newline, word, and byte counts for each file 12 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Options in the examples

-l, --lines
print the newline counts
-w, --words
print the word counts
-c, --bytes
print the byte counts
-m, --chars
print the character counts

Examples

wc /etc/passwd
wc -l /etc/passwd

Line count only

wc -w file.txt

Word count only

wc -c file.txt

Character/byte count

wc -m file.txt

Character count (multibyte aware)

wc -l *.txt

Count lines in all .txt files

ls /etc | wc -l

How many files in /etc?

cat file.txt | wc -w

Count words via pipe

ps aux | grep nginx | wc -l

Count nginx processes

wc -l < ~/logs/access.log

How many lines = how many requests

awk '$9 == 404' ~/logs/access.log | wc -l

Only the lines whose 9th field is 404

journalctl -u sshd -p err | wc -l

Lessons that use wc

Related commands