Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I usually do some grep when developing, and there are some extensions that I always don't want to look for (like *.pyc).

Is it possible to create a ~/.egreprc or something like that, and put some filtering to always not show pyc files in result.

Is this possible, or will I have to create an alias for using grep in this manner, and call the alias instead of grep?

share|improve this question

3 Answers 3

up vote 22 down vote accepted

There's no rc file for grep, but you can set the environment variable GREP_OPTIONS to a whitespace-separated list of options that will apply to all grep commands. This requires GNU grep (the implementation on non-embedded Linux, Cygwin and some other systems) 2.4; the --exclude option is new to GNU grep 2.5. Put something like this in your ~/.profile:

export GREP_OPTIONS='--color=auto --exclude=*.pyc --exclude-dir=.git'

If you've done this but want to run grep once without those default options, you can override them (e.g. grep --color=never to force colors off) or empty the GREP_OPTIONS variable by running GREP_OPTIONS= grep ….

Another possibility is to define aliases for grep. I use a variant of the following:

alias regrep='grep -Er --exclude=*~ --exclude=*.pyc --exclude-dir=.bzr --exclude-dir=.git --exclude-dir=.svn'
share|improve this answer
    
This is THE answer I was looking for. I think I'll read more about ~/.profile. @Gilles, you're the man. Thanks for helping! –  Somebody still uses you MS-DOS Feb 25 '11 at 20:26
    
Considering there are two possibilites to do this (GREP_OPTIONS and alias grep): Is one of them preferred? Why or why not? –  Legate Feb 27 '11 at 19:54
2  
@Legate: An important difference is that GREP_OPTIONS will affect grep calls in scripts, while an alias won't. So GREP_OPTIONS should be used sparingly. The most common use is for --color=auto, which is in principle harmless in scripts because it only takes effect if the output is a terminal. –  Gilles Feb 27 '11 at 20:14

ack can do what you want and more.

It will ignore binary files by default and you can tell it to ignore other filetypes as you want --notext, --nohtml, etc. It has the ability to define an rc file too so you can customize it with your own types.

share|improve this answer
    
I'm using ack too, but I didn't like it. It's indeed fast, but it's not available in all installations, so I was thinking about grep. Thanks! –  Somebody still uses you MS-DOS Feb 25 '11 at 19:23
1  
ack is a single Perl program, downloadable as a plain text file. If you want, you can go to betterthangrep.com/ack-standalone and cut & paste the contents into a file. I specifically made it so that it is available anywhere you can run Perl. –  Andy Lester Feb 26 '11 at 3:52

Not in GNU grep/egrep, there isn't.

You probably want a specially-named alias or script to do this anyway, because someday later you might find yourself very confused when trying to look in something that you forgot matches your special configuration.

share|improve this answer
3  
GREP_OPTIONS in ~/.profile comes close. –  Gilles Feb 25 '11 at 20:06
1  
@Gilles — good point. In fact, I forgot that I have that set myself for --color=auto! –  mattdm Feb 25 '11 at 20:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.