title: How to print number of occurances of a word in a file in unix
body: <p>This is my shell script.
  Given a directory, and a word, search the directory and print the absolute path of the file that has the maximum occurrences of the word and also print the number of occurrences. 
  I have written the following script</p>
  
  <pre><code>#!/bin/bash
  
   if [[ -n $(find / -type d -name $1 2&gt; /dev/null) ]]
   then
    echo "Directory exists"
  
     x=` echo " $(find / -type d -name $1 2&gt; /dev/null)"`
     echo "$x"
     cd $x
     y=$(find . -type f | xargs grep -c  $2  | grep -v ":0"| grep -o '[^/]*$' | sort -t: -k2,1 -n -r ) 
     echo "$y"
     else 
     echo "Directory does does not exists"
   fi   
  
  
   result: scriptname directoryname word
   output: /somedirectory/vtb/wordsearch : 4
           /foo/bar: 3
  </code></pre>
  
  <p>Is there any option to replace xargs grep -c $2 ? Because grep -c prints the count=number of lines which contains the word but i need to print the exact occurrence of a word in the files in a given directory </p>