Greg Hinkel's UNIX Tip of the Week for March 3, 1996

Here's how you can use grep to recursively search for a string.  In other
words, this is how you can search for a string in all files at and below the
current directory.  It is not case sensitive (note the -i switch to grep).

The first example will search for the string "Find Me" in all the files that 
end with .c at and below the current directory. 

find . -name "*.c" -exec grep -i "find me" {} /dev/null \;


The next example will search for the string "cpulimit" in all the system 
include files.  We'll make this one case sensitive.

find /usr/include -name "*" -exec grep cpulimit {} /dev/null \;


You could make your own "rgrep" (recursive grep) by doing the following (put 
it in your ~/.cshrc file for C shell users and be sure to "source .cshrc" or
logout and login again).

alias rgrep find . -name \!:2 -exec grep -i \!^ {} /dev/null \\\;

then use something like the following to invoke it.

rgrep "STRING" "*.c"

Examples (assuming you created an alias for rgrep).
rgrep "puddy tat" "*"
rgrep mumble "*.txt"

Make sure you use the double quotes if STRING has any special characters, and
if you use wildcards in the file specification.

Next Tip of the Week · Index

Greg's Home Page


Greg Hinkel / (hinkelgc AT ornl.gov)
Last Modified: Friday, 02-Sep-2005 12:52:12 EDT
Visitors: 31801 since March 18, 1996