Linux commands: wc
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
Here is how can I help you:
- COURSES where I teach everything I know
- THE VALLEY OF CODE your web development manual
- BOOTCAMP 2024 cohort in progress, next edition in 2025
- BOOKS 16 coding ebooks you can download for free on JS Python C PHP and lots more
- SOLO LAB everything I know about running a lifestyle business as a solopreneur
- Interesting links collection
- Follow me on X