Find and Grep

My good friend Prentice posted a blog entry about using grep recursively. While I agree, this is pretty cool, I've recently fallen in love with using find to find things (what a crazy concept) in my code.

I got this little trick from one of my coworkers, which I now use to find just about anything in my code repositories:

gfind "*php" "code I want"

You'll notice that that line wont work for you, so put the following in your ~/.bashrc file.

gfind () { 
   find . -name "${1}" -exec grep -Hin ${3} "${2}" {} \; ; 

This means that it will only look at files that end in php, so of course it will stay out of your .svn or .git directories.

But of course this isn't the best option. What if you want to call the following:

gfind "*" "search string"

You need to change the gfind definition to do the equivalent of what Prentice was doing, but with find.

gfind () {
   find Code -name "${1}" -a ! -wholename '*/.*' -exec grep -Hin ${3} "${2}" {} \; ; 

If you want to learn more about find or grep you can check out the man pages on your system or the Wikipedia pages for the find command or the grep command.