Execution problems

How to find a word in a directory which contains many files?
i just want to count how many such words are present in all the files?
This is the code which i tried for a single file

echo "Enter the file name:"
read file
echo "Enter the word to search:"
read word
if [ -f $file ]
then
echo "The count is :"
grep -o $word  $file | wc -l
else
echo "The word doesn't exist"
fi

The grep command has the -c flag to help you count without needing to pipe the output to the wc command.

Do you want to search all files in a particular directory? What about subdirectories?

Kind regards,
Robin

i just need to print the counts for each file in a directory
how can i execute this using for loop?
in my case there is no subdirectory

---------- Post updated at 07:46 PM ---------- Previous update was at 07:43 PM ----------

echo "Enter the word to search:"
read word
for (( i=1; i<=10; i++ ))
do
if [ -f $file ]
then
count=grep -o $word $file | wc -l
echo "$i '/t' $file '/t' $count " 
else
echo "The word doesn't exist"
fi
done

Hi
What is the use of loop to check 10 times and if condition ?
Try below one:

 grep -o -c $word * 

If you have subdirectory or files under it then add -r option.

So to loop for each file in the current directory it is like this:-

for file in *
do
   echo "Working on $file"          # Whatever you need to do to each file here
done

To count the occurrences of $word you can use the line:-

   grep -co $word $file

If you want the total for all the files in the directory, you might get away with (no loop required):-

grep -co $word *

You might have to merge all the files together to avoid getting individual file counts (again, no loop):-

cat *|grep -co $word

I hope that this is useful,
Robin