38

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?

67

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'
  • 1
    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
  • 1
    Considering there are two possibilites to do this (GREP_OPTIONS and alias grep): Is one of them preferred? Why or why not? – Christoph Wurm Feb 27 '11 at 19:54
  • 4
    @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 'SO- stop being evil' Feb 27 '11 at 20:14
  • GREP_OPTIONS is deprecated; please use an alias or script – Steven Penny Nov 20 '16 at 13:39
3

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.

  • 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
  • 2
    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
0

Not a direct answer to your question. But grep has an option to ignore all binary files including *.pyc. The option is -I

grep -rI hello .
0

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.

  • 3
    GREP_OPTIONS in ~/.profile comes close. – Gilles 'SO- stop being evil' 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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