using GREP to search specific file types

Every developer knows and loves GREP, the great search utility available on all Unix-based systems; while it does a great job searching, the options are a little lacking. One such option is restricting the search to only specific file types/extension. Luckily that functionality can be easily wrapped up in a handy script.

Create the script file:

cd /bin
sudo vim grepp

(You can name the script whatever you want, I named mine "grepp" for easier typing!)

Paste the following code:

grep $1 $(find . -name '*.php' -or -name '*.inc' -or -name '*.tpl')

(To search only .php, .inc and .tpl files.)

Save and quit:

[ESCAPE]
:w [ENTER]
:q [ENTER]

Set the file permissions:

sudo chmod 0777 grepp

Run the script within any directory you'd like to search in:

grepp "foo bar"

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
Mollom CAPTCHA
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated.