Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.

UNIX Support: Recursively GREP

I've always had two major problems with the UNIX "grep" command. First, the "grep" command will gladly read a non-text file, and thus produce garbage if the grep pattern was found. Second, the "grep" command can be used to search in all the files in a single directory, but the command will not recursively search all sub-directories. As a result, I wrote a Perl script, called recursive grep or "", to resolve these issues.

Here is the syntax to run this script.

$ <WORD> [PATH1] [PATH2] ... [OPTIONS]

The first argument is the only required argument for the value to search or grep for. The optional "PATH" arguments that can be next are the directories where to start to recursively search for text files to grep. The default path is the current directory. Finally, the last optional argument is for these options:

"-verbose" displays the matched lines for each file searched.
"-list" lists the files that have matching text.
"-edit" opens the matching files in an editor.
"-nocase" performs a case insensitive search.
"-pattern=STR" only searches files that have filenames containing a string of user's choice.

Alternatively, you can just use the "-help" argument to see the usage and some examples of how to use this script.

You can download this Perl script here, or you can copy-n-paste from down below. I would like to thank Monty Scroggins for enhancing this script with more options and teaching me a bit more Perl.

by Phil for Humanity
on 20081118

Related Articles
 » Recursive GREP
 » How to Implement "grep v" in Perl
 » Command Loop