rgrep

Here's a very helpful recursive grep commandline script I use on OSX and Linux all the time.

To use, just cd to the starting directory and type rgrep string. This will find all occurrences of "string" in the starting directory and any of its subdirectories.

Copy the code below and write it to a file called 'rgrep'. Make sure rgrep is somewhere in your path. (Maybe under ~/bin.)


#!/usr/bin/perl
$| = 1;
my ($str) = @ARGV;
if (!$str) {
die "Syntax: $0 {str} # finds {str} starting in current dir.\n";
}
my $cmd = "find . -exec grep -H -n '$str' {} \\;";
open(CMD,"$cmd|") or die "Unable to open CMD: $!\n";
while(<CMD>) {
print $_;
}

As you can see, this command depends on the system's find and grep commands.

Post a comment to let me know what you think!