how to count total number of words in a file?

The command wc aka. word count can do it:

$ wc -w <file>

example

$ cat sample.txt
today is a 
good day


$ wc -w sample.txt
5 sample.txt


# just the number (thanks to Stephane Chazelas' comment)
$ wc -w < sample.txt
5

I came up with this for JUST the number:

wc -w [file] | cut -d' ' -f1

5

I also like the wc -w < [file] approach

Finally, for storing just the word count in a variable, you could use the following:

myVar=($(wc -w /path/to/file))

This lets you skip the filename elegantly.


The better solution is using Perl:

perl -nle '$word += scalar(split(/\s+/, $_)); END{print $word}' filename

@Bernhard

You can check the source code of wc command from coreutils, I test in my machine, with file subst.c in bash 4.2 source.

time wc -w subst.c

real    0m0.025s
user    0m0.016s
sys     0m0.000s

And

time perl -nle '$word += scalar(split(" ", $_)); END{print $word}' subst.c

real    0m0.021s
user    0m0.016s
sys     0m0.004s

The bigger the file is, the more efficient Perl is with respect to wc.