AI Workshop: learn to build apps with AI →
Text Processing: wc - Word Count

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


The wc command gives us useful information about a file or input it receives via pipes.

echo test >> test.txt
wc test.txt
1       1       5 test.txt

Via pipes, you can count the output of the ls -al command:

ls -al | wc
6      47     284

The first column returned is the number of lines. The second is the number of words. The third is the number of bytes.

We can tell it to just count the lines:

wc -l test.txt

or just the words:

wc -w test.txt

or just the bytes:

wc -c test.txt

Bytes in ASCII charsets equate to characters, but with non-ASCII charsets, the number of characters might differ because some characters might take multiple bytes, for example this happens in Unicode.

In that case the -m flag will give you the correct character count:

wc -m test.txt

The wc command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

Lessons in this unit:

0: Introduction
1: grep - Search Text
2: find - Search Files
3: sort - Sort Lines
4: uniq - Remove Duplicates
5: diff - Compare Files
6: ▶︎ wc - Word Count
7: xargs - Build Command Lines