Monday, March 22, 2010

Digging directories: Finding files recursively which match a certain pattern

"grep" can search a particular file or files in current directory for
matching a pattern. 

If anybody wants to search files recursively from the current directory
which match a particular pattern
the following script can be used.

Put the following script in a file.


if [ $# -ne 2 ]
then
   echo "Usage : $0  "
   echo
   exit
fi

PWD=`pwd`
for file in `find $PWD -name "$1"`
do
  grep -l "$2" $file
done

 
For Example ,if you want to find a string called "execption" in all *.java
files recursively under the directory.

scriptfilename *.java exception

------------------------------------------------------------------------------------------------------


find . -name *.java -exec grep -Hn "SEARCH_STRING" {} \;
-----------------------------------------------------------------------------

No comments:

Post a Comment