My most common grep line is just..

grep -IRl "text" *

However I'm kinda getting tired of retyping this over and over - is there some way I can make an alias command so that those arguments are always enabled?

And, I was wondering what arguments you usually specify for text searching - my two arguments 'R' for recursion, 'I' for not including binary types like jpg/gif, and 'l' for line number seem a bit too minimal. Which arguments do you use?

link|improve this question
This probably belongs on SU. – Zifre Jul 27 '09 at 18:49
I'm sure the su crowd will be very, very pleased. – innaM Jul 27 '09 at 18:50
sorry, I keep forgetting that's a subjective tag - added. – meder Jul 27 '09 at 18:55
Not directly answering your question but ack claims to have good defaults (and has a ~/.ackrc) for search purposes. betterthangrep.com – Jeffrey Jose Dec 29 '10 at 8:11
feedback

migrated from stackoverflow.com Jul 27 '09 at 18:56

This question came from our site for professional and enthusiast programmers.

15 Answers

up vote 13 down vote accepted

Check out the GREP_OPTIONS environment variable. More info is in man grep:

GREP_OPTIONS

This variable specifies default options to be placed in front of any explicit options. For example, if GREP_OPTIONS is '--binary-files=without-match --directories=skip', grep behaves as if the two options --binary-files=without-match and --directories=skip had been specified before any explicit options. Option specifications are separated by whitespace. A backslash escapes the next character, so it can be used to specify an option containing whitespace or a backslash.

link|improve this answer
awesome, I ended up using this. – meder Aug 14 '09 at 4:34
I worry that GREP_OPTIONS affects every invocation of grep, so if I call a tool which internally calls grep, then grep will not behave as the tool expects, and the tool will break. Is this a real problem? – Jonathan Hartley Mar 17 at 12:30
feedback

I also use --color, to color the output, and sometimes -Bn and -An, where n is a number, to show a number of lines before and after the matched lines, thus giving context.

link|improve this answer
1  
you could also use -Cn to combine the -A and -B options for Context :) – warren Aug 31 '09 at 8:37
feedback

You can add this to your .bashrc:

alias grep="grep -IRl"
link|improve this answer
4  
I'd have preferred adding a letter to the name or something. And not overriding the original name – Avihu Turzion Jul 27 '09 at 18:49
2  
It seems like the correct way is to specify GREP_OPTIONS - or do some people still use alias? – meder Jul 27 '09 at 18:54
1  
I use shell alias. There is nothing wrong with them. – bortzmeyer Jul 28 '09 at 7:05
Many linux distributions ship with alias rm="rm -i" in the .bashrc. You may as well use GREP_OPTIONS but I don't think the alias is bad. – John Fouhy Jul 28 '09 at 22:20
My problem with using an alias is that I want to be able to call grep from programs such as vim, but vim doesn't use a shell on the commands I issue, so it doesn't see this alias. I guess I could create a vim command which does explicitly use a shell to invoke grep. Hmmmmm, I'll go try it... – Jonathan Hartley Mar 17 at 12:32
feedback

I often use -n, which prints the line number of the match in the file. This is particularily useful in conjunction with -H, which prints the name of the file that matched (this defaults to ON for multi-file searches).

So, for example, if call grep -Hn 'foo' * in a directory full of files, I get output similar to this:

foo.txt:34:line that matched that contains foo
foo.txt:51:another line with foo
bar.c:4:int foo;
link|improve this answer
feedback

If find the following quite handy, to search my source tree while ignoring SVN metadata files:

alias grepnosvn="grep -r --exclude-dir=.svn"
link|improve this answer
feedback

is there some way I can make an alias ...

See alias.

link|improve this answer
feedback

Best grep is clean grep.

Example: grep stdio *

link|improve this answer
1  
Why is that so? – Sinan Taifour Jul 27 '09 at 18:52
feedback

I use whatever I need in the given situation. If I need to grep recursively, I use -r. If I need to search cases-insensitively, I use -i.

The only option of grep that I use each time, is --color. But Sinan Taifour already pointed that one out.

link|improve this answer
feedback

I use

grep -i

for case insensitive search

grep -l

to just list the file names

grep -n

To list line numbers of the occurence in the file names

grep -v

To list the files that don't contain the pattern

link|improve this answer
feedback
  • -R for recursive searching
  • -A 2 and -B 2 to display the 4 lines around the matched-line (2 before, 2 after)
  • -i to ignore case

For example:

grep -Ri -A2 -B2 /some/dir somethingtomatch
link|improve this answer
feedback

The only option I specify by default, by using an alias, is using colour to highlight matches:

alias grep='grep --color=auto'
link|improve this answer
feedback

Regarding your first question, other people mentioned GREP_OPTIONS and bash alias.

Yet another way is to use a shell script to run grep with your favorite options.

Create the file ~/bin/mygrep with following contents:

#!/bin/bash
grep -IR1 "$@"

Then you can run

mygrep hello file1 file2

which is effectively same as

grep -IR1 hello file1 file2

When you try out or switch to shells other than bash, such as fish or zsh, your mygrep will be available there too.

You can also make myls, myls2, myrsync, myperl and so on.

link|improve this answer
feedback

grep -Hni

Actually, egrep -Hni. The "e" is for the extended version of grep which supports a more powerful pattern matching language.

The -Hni form is really useful for jumping quickly to matches because it produces the "Emacs format" that is supported by many environments. The format looks like this:

<path>:<line-number>:<column-number>: <match>

An example looks like this:

/some/path/to/a/file:4:6: foo

Sure, -R can be thrown in as well to descend recursively into subdirectories, but I usually use xargs to supply filenames to grep from find:

find . -type f -name '*some-filename-pattern*' -print0 | xargs -0 -n 1 egrep -Hni some-search-pattern

Of course, this can all be packaged into a script (with absolute path names for the executables).

link|improve this answer
I use: -exec grep \{\} \; as argument to find, I have never quite get how xargs does works ( nor feel like reading the doc ) – OscarRyz Aug 5 '09 at 19:20
feedback

Actually I ended up aliasing 'g' to 'grep -RlI' because those extra arguments can sometimes offset something, example being 'ps aux | grep pidgin' returned ( standard input ) instead of what I wanted. Plus it's less stuff to type. Thanks for all the answers.

link|improve this answer
feedback

Take a look at ack. It is optimized specifically for cases like yours.

grep -IRl "text" *

in ack would be

ack text

By default, ack ignores binary files, version control directories, and many others. It also by default recurses down the directory tree and shows line numbers.

It's a Perl program that you can download and put into your ~/bin directory and you don't have to install anything else, so no need for admin rights. And it runs the same on Windows.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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