I'd like to share a simple search script this week, basically a 'find' and 'grep'.
   Even though it's a simple script, I find (no pun intended) that I use
   it frequently to search for a text string buried in a sub-directory.



   #!/bin/sh
   pwd=`pwd`
   echo "Searching for '$1' in \"${pwd}\"" ...
   for i in `find $pwd`; do
     if [ -f $i ]; then
       grep -i "$1" $i > /dev/null
       if [ $? -eq 0 ]; then
         echo $i
       fi
     fi
   done

   Usage:
   # findit.sh [match string]

   For example:
   # findit.sh isReachable
   Searching for 'isReachable' in "/export/home/jayd/trunk/Common/UCUtils" ...
   /export/home/jayd/trunk/Common/UCUtils/src/com/sun/uc/utilities/Ping.java
   /export/home/jayd/trunk/Common/UCUtils/build/classes/com/sun/uc/utilities/Ping.class
   /export/home/jayd/trunk/Common/UCUtils/dist/UCUtils.jar

Comments:

what about just find $PWD | xargs grep string?

Posted by xargs on July 11, 2008 at 10:09 AM EDT #

whats wrong with this:

find ./ -exec grep -il FOO {} \;

seems a lot shorter :-)

Posted by John on July 11, 2008 at 10:12 AM EDT #

Or even just
/usr/sfw/bin/gegrep -R "string" *

;)

Posted by mike wallis on July 11, 2008 at 10:40 AM EDT #

Thanks for the comments, and good alternatives ...
Regarding the find . -exec approach ... /dev/null is needed to trick grep into displaying the file name, which changes the query to: 'find `pwd` -exec grep -i "$1" {} /dev/null \;'

Posted by Jay on July 11, 2008 at 05:45 PM EDT #

grep -l displays the filename; this is an exact equivalent:

#!/bin/sh--
/usr/sfw/bin/ggrep -ril "$*"

Posted by Ceri Davies on July 12, 2008 at 05:17 PM EDT #

Run time() against the find -exec and find | xargs grep. The find.xargs.grep is lightyears faster.
--dio

Posted by Dio on July 18, 2008 at 11:26 AM EDT #

you can get the filename in find w/ -H option:

find / -type f -exec grep -Hi bar {} \; 2>/dev/null

Posted by Rick Dennis on August 18, 2008 at 01:35 PM EDT #

Hi,
I found another alternative which requires less resource. The command is "find . | xargs grep -li search_string"

serengeti: /tmp/mc:>timex find . | xargs grep -li kanuni | wc -l

real 42.00
user 0.05
sys 0.16

3395
serengeti: /tmp/mc:>timex /tools/findit.sh kanuni | wc -l

real 2:09.18
user 47.53
sys 13.51

3396
serengeti: /tmp/mc:>

Posted by Mustafa Celikoz on December 22, 2008 at 07:52 AM EST #

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by Jay Danielsen