Counting words

Hi
Is there a way to count the no. of words in all files in directory. All are text files.I use wc -w but somehow i am not getting the rite answer.
Is there an alternative.
Thanks in advance

Hi,

Not sure what you mean by "not getting the rite answer for wc -w". Well, since you want the word count of all files in the dir, I would try the below code -

cd YOURDIR
ls -1 > /tmp/files

while read files
do

   COUNT=`wc -w $files`
   echo $COUNT

   #you can do it even without using a variable

done < /tmp/files 
rm /tmp/files

What is the output of

wc -w *

and why is it not what you expected?

Hi
I got the problem. That's because my code is ignoring the sub directories. Thanks all for your help

you mean like this? Or do you want the grand total as well?

find -type f -exec wc -w {} \;

I need the grand total as well

Like this then?

find -type f -exec wc -w {} \;|awk '{tot+=$1}1;END{print tot" total"}'

Perfect that worked like a charm. Also is there a way to suppress the output of the awk and just display the grand total. The output just looks clumsy

Sure:

find -type f -exec wc -w {} \;|awk '{tot+=$1};END{print tot}'
find  . -type f -exec wc -w {} \; | nawk '{a+=$1} ; END{print a;}'