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?

oalders
  • 103
  • 4

5 Answers5

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
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.

Mark McKinstry
  • 14,943
  • 4
  • 34
  • 27
  • 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 http://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 03: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 .
Tyler Liu
  • 129
  • 3
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
$
sage
  • 111
  • 4
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.

heemayl
  • 54,820
  • 8
  • 124
  • 141
mattdm
  • 39,535
  • 18
  • 99
  • 133