I frequently will do something like grep -R foo .
at the root of a project. It’s helpful but the results get diluted by the inclusion of binary. temp, and test files that I don’t care about. So I end up affixing 2>&1 | grep -vE “Binary|/tmp/|/test/“
, which filters out the stuff I don’t care about. Problem is, its a pain to type each time. Everything including and after the 2>&1 stays the same from query to query. So I want to put it in an alias of sorts that would let me grep -R foo . <thingie>
, where thingie is something fairly short. I’ve tried aliases and environment variables but aliases only work if theyre the first word on the line, and environment vars I can expand with ${thingie}
but the piping seems to get screwed up in the process. Any ideas on how to make this work?
1
-
1If I understand your requirements, you could write a function and install the function in a bash start up file. – fd0 Jun 16 '15 at 14:42
2
Yes a function is the way to go. If you have GNU grep
- use the
--exclude-dir
option so the recursive search does not even bother with it. - use the
--binary-files=without-match
option to ignore binary files (alias-I
).
So:
mygrep() {
grep -R --exclude-dir=tmp --exclude-dir=test -I "$@" .
}
Then call
mygrep Foo
mygrep -i foo
mygrep -E '(foo|bar)baz'