wc
on the command line
Setup for the below demo
The files file1.txt
, file2.txt
and file3.txt
contain the following:
this is a test
i hope this works!
Count Lines
Use wc -l
to count the number of lines.
Examples:
$ wc -l file.txt
>>> 2 file.txt
$ echo 'this is a test\ni hope this works!' | wc -l
>>> 2
Count Words
Use wc -w
to count the number of words.
Examples:
$ wc -w file.txt
>>> 8 file.txt
$ echo 'this is a test\ni hope this works!' | wc -w
>>> 8
Count Characters
Use wc -c
for character count.
This one seems to return a few extra characters... but is close enough to be useful!
Examples:
$ wc -c file.txt
>>> 34 file.txt
echo '12345' | wc -c
>>> 6
I'm not quite sure why it's 6, though... ⚠️ It's useful enough as a proxy, but not sure I'd trust this...
Count multiple files or a folder
Example, many files:
$ wc file1.txt file2.txt file3.txt
>>> 2 8 34 file1.txt
2 8 34 file2.txt
2 8 34 file3.txt
6 24 102 total
Example, a folder (in this case, the current folder .
):
$ wc $(find . -type f)
>>> 2 8 34 ./file2.txt
2 8 34 ./file3.txt
2 8 34 ./file1.txt
6 24 102 total