Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a command to count number of all words in a file. For instance if a file is like this,

today is a 
good day

then it should print 5, since there are 5 words there.

share|improve this question
Have you tried wc -w $FILE ? – don_crissti Jun 19 '13 at 17:07
Don't solve what's been solved. – Prasanth Jun 19 '13 at 17:31
up vote 27 down vote accepted

The command wc aka. word count can do it:

$ wc -w <file>


$ 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
share|improve this answer
Note that words for wc -w don't have the same definition as for GNU grep -w. For wc a word is a sequence of one or more non-space characters ([:space:] character class in the current locale). For instance foo,bar and foo bar (with a non-breaking space) are each one word. – Stéphane Chazelas Jun 12 '14 at 15:18

I came up with this for JUST the number:

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


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.

share|improve this answer
wc -w < "$file" for JUST the number. – Stéphane Chazelas Jun 19 '13 at 20:24

The better solution is using Perl:

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


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


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.

share|improve this answer
Why is this better than wc? – Sparr Jun 19 '13 at 17:13
wc need open entire file before it processes, it will cause problem when you work with large file. – cuonglm Jun 19 '13 at 17:20
@Sparr for one thing because, to my very great surprise, it seems to be much faster. I tried it on a text file with 141813504 words and wc took ~14sec while Perl took ~5sec! – terdon Jun 19 '13 at 17:28
I think the 'bigger' issue really is an answer that has a dependency on Perl and I'm never a big fan of such a dependency. If the question was about performance that would be another thing. – Michael Durrant Jun 19 '13 at 17:31
Note that a split on /\s+/ is like a split(' ') except that any leading whitespace produces a null first field. That difference will give you one extra word (the null first field, that is) per line link. So use (split(" ", $_)) otherwise for a file created like this: echo -e "unix\n linux" > testfile your one-liner reports 3 words. – don_crissti Jun 19 '13 at 18:03

Let's use AWK!

$ function wordfrequency() { awk 'BEGIN { FS="[^a-zA-Z]+" } { for (i=1; i<=NF; i++) { word = tolower($i) words[word]++ } } END { for (w in words) printf("%3d %s\n", words[w], w) } ' | sort -rn } 
$ cat your_file.txt | wordfrequency

This lists the frequency of each word occurring in the provided file. I know it's not what you asked for, but it's better! If you want to see the occurrences of your word, you can just do this:

$ cat your_file.txt | wordfrequency | grep yourword

I even added this function to my .dotfiles

Source: AWK-ward Ruby

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.