47

I usually use grep when developing, and there are some extensions that I always want to exclude (like *.pyc).

Is it possible to create a ~/.egreprc or something like that, and add filtering to exclude pyc files from all results?

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

5 Answers 5

79

No, there's no rc file for grep.

GNU grep 2.4 through 2.21 applied options from the environment variable GREP_OPTIONS, but more recent versions no longer honor it.

For interactive use, define an alias in your shell initialization file (.bashrc or .zshrc). I use a variant of the following:

alias regrep='grep -Er --exclude=*~ --exclude=*.pyc --exclude-dir=.bzr --exclude-dir=.git --exclude-dir=.svn'

If you call the alias grep, and you occasionally want to call grep without the options, type \grep. The backslash bypasses the alias.

6
  • 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! Commented Feb 25, 2011 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? Commented Feb 27, 2011 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. Commented Feb 27, 2011 at 20:14
  • 2
    GREP_OPTIONS is deprecated; please use an alias or script
    – Zombo
    Commented Nov 20, 2016 at 13:39
  • @oalders I've updated my answer. Commented Apr 24, 2020 at 20:28
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.

2
  • 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! Commented Feb 25, 2011 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. Commented Feb 26, 2011 at 3:52
2

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 .
1

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.

2
  • 3
    GREP_OPTIONS in ~/.profile comes close. Commented Feb 25, 2011 at 20:06
  • 1
    @Gilles — good point. In fact, I forgot that I have that set myself for --color=auto!
    – mattdm
    Commented Feb 25, 2011 at 20:14
1

Since at least 2016 (according to Steven Penny's comment), the grep-recommended way of doing this is with an alias or script:

$ export GREP_OPTIONS="--exclude='*~'"
$ grep -r someStringThatDoesNotExist
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .