Filtering out SVN directories from grep output

I often need to use the tool grep to search for specific text under a directory tree in Linux. For example, I might want to search for every occurrence of the text "helper" in my latest web site source code. But when the source code is under Subversion, or SVN, version control, the results will be cluttered with more matches than you probably want.

This is because of the text contained in the hidden .svn directories. Take, for example, the following grep command:

$ grep -r "helper" . ./_edit_form.php:use_helper('MySite'); ./.svn/text-base/_edit_form.php.svn-base:use_helper('MySite');

This searches for every occurrence of "helper" in the current directory and those below it (note the -r option, which makes the command recursive). It finds matches in 2 files - but one of the matches is in an SVN directory that I will not be editing, and which is there for the purpose of the SVN client only. In order to filter out such results you can pipe the output of the grep command to another grep command that searches for lines that do not match the text ".svn", by using the -v option, which inverts the match:

$ grep -r "helper" . | grep -v ".svn" ./_edit_form.php:use_helper('MySite');

It may seem to be of limited value when neither command yields many results, but it can be very useful when many matches appear in the SVN directories, sometimes causing the output of the command to take up more than one screen.

Of course, the principle can be applied to any other situation in which files containing a certain string (in this case ".svn") in their full path names need to be omitted from the results.