Link to home
Start Free TrialLog in
Avatar of Alex_Tong
Alex_Tong

asked on

Searching for a word within a list of files

Given a list of files, how can I find a specific word or phrase?

i.e.  something like...
find . -name myfile |grep -i evil_subroutine *

I want to look for all occurances of myfile and search all occurances for "evil_subroutine"

-Alex
Avatar of bira
bira

I would suggest you to use a script, like this:

  for i in `find . -name myfile`
  do
  echo "Processing" $i
  grep "evil_subroutine" $i
  done
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was wierd... I said "Sumbit" and only part got posted... Let's try again...

You can do it with find. If you want to find all the strings in all of the files, do:

find . -name myfile -exec grep -i evil_subroutine {} \;

Or if you want to know only what files contain the string do:

find . -name myfile -exec grep -il evil_subroutine {} \;

The standard operators for find apply so to look in C source files you might do:

find . -name "*.c" -exec grep -i evil_subroutine {} \;

Or if there isn't a pattern to the file names you can avoid grepping directories with:

find . -type f -exec grep -i evil_subroutine {} \;

See "man find" and "man grep" for more information.
Avatar of Alex_Tong

ASKER

Fantastic answer.
Thanks jlevie