What is the Best way to search files for a string??

I'm looking to seach all the files in a directory and sub-directories looking for a string. When the string is found, I want to display the filename and the entire line of that file that the string was found on.

what is the best way to do this ?? I've been playing around with awk, find, and grep with no luck. I am on HPUX 11.11

thanks

Ok, I'm getting closer. I found a grep command that is somewhat working.......

grep -i 'string' *

This is giving me what I need, but is only searching the present directory.

How do I get this command to also search the sub-directories ??

Try something like:

grep -i <pattern> `find` 

Regards

Thanks for the response Franklin.

That code gives an error :

grep: can't open find

It thinks find is a filename.

Did you use backticks around find? You can also try it this way:

grep -i <pattern> ${find}

Regards

I didn't use the backsticks the first time.....but received another error when using them in which I needed to CNTL C out of.

I copied my cmd prompts this time. The first command shows the grep command working on 2 test files that I have. I also have a sub-directory in /home/mark with another test file that I want it to find............

[unix] mark[426]: pwd
/home/mark
[unix] mark[427]: grep -i 'monkey' *
find1.test:this line contains the word monkey.
find2.test:this line contains the words monkey and turtle.
grep: can't open oldetc

[unix] mark[428]: grep -i 'monkey' `find`
Usage: find path-list [predicate-list]

[unix] mark[429]: grep -i 'monkey' ${find}
sh: find: Parameter not set.

[unix] mark[430]:

Perhaps your find version has a problem with it, try:

grep -i <pattern> `find . -name "*"`

Regards

I think that will work. The live test I did locked me up.....but I think because it found the string in an executable file and tried to display it.

If I only wanted to search C source files (test.c) would I use.....

grep -i <pattern> `find . -name "*.c"`

I anticipated the solution on the first answer but for the completeness, find is mostly used this way:

find . -name "*" -exec grep -i <pattern> {} \;

Regards

Keep It Simple :slight_smile:

grep -ir 'string' *

the -ir gives me an error :frowning:

[unix] mark[449]: grep -ir 'monkey' *
grep: illegal option -- r
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
[-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
-f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]
[unix] mark[450]:

This code did serach the sub directories and display the line in the file......BUT now does NOT include the file name containing the string :mad:

[unix] mark[24]: pwd
/home
[unix] mark[25]: find . -name "*.c" -exec grep -i 'monkey' {} \;
find: cannot open ./mark/oldetc
this line contains the word monkey.
this line contains the words monkey and turtle.
[unix] mark[26]:

Ups... sorry. I've not read carefully, that you use HPUX.

Add a /dev/null, or see if your grep has an option to force it to display the file name even if it only greps one file.

find . -name "*" -exec grep -i <pattern> /dev/null {} \;

Oh, the -name "*" part is probably redundant. Try to leave it off and see if your find chokes.

It's surprising what people will get themselves into just so they don't have to download, build, and install GNU greputils. That would be my recommended solution for your inevitable follow-up problems.

A more elegant way is to use a script with a loop where you have more controle over the output.
It should be something like:

find *.c | while read line
do
  grep -i 'monkey' >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo Found in "$line"
  else
    echo Not found in "$line"
  fi
done

Regards

Please don't spread this anti-pattern.

if grep -i 'monkey' >/dev/null 2>&1; then ...