Skip to content

Linux commands: wc

New Course Coming Soon:

Get Really Good at Git

A quick guide to the `wc` command, used to count lines, words or bytes

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

Example via pipes, we can count the output of running 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 this case the -m flag will help getting the correct value:

wc -m test.txt

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

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!

Here is how can I help you: