The option -n makes grep display the line number in front of the line that contains the given pattern. For example:
[amit@dslamit amit]$ grep color .bashrc
alias ls='ls --color=auto'
[ -f /etc/profile.d/color_ls.sh ] && source /etc/profile.d/color_ls.sh
# setup for color ls
[amit@dslamit amit]$ grep -n color .bashrc
14:alias ls='ls --color=auto'
23: [ -f /etc/profile.d/color_ls.sh ] && source /etc/profile.d/color_ls.sh
60:# setup for color ls
[amit@dslamit amit]$
The option -v inverts the search by finding lines that do not match the pattern.
The option -i asks grep to ignore case in the search string.
A very powerful option is the recursive search option, -r, that will make grep search recursively in a directory or directories. For example, the following command searches for all files with the string ``Crow'' in them.
[amit@onyx examples]: grep -r "Crow" C-examples/ C-examples/plugins/plugin1.c:/* Author: Dan Crow C-examples/plugins/plugin2.c:/* Author: Dan Crow C-examples/plugins/runplug.c:/* Author: Dan Crow (modified by Amit Jain) [amit@onyx examples]:
Programmers often use this option to quickly search for a declaration of a structure in a large project consisting of hundreds or thousands of files in many directories and subdirectories. This is often called ``grepping'' the source! See the following for an example.
[amit@onyx examples]: grep -rn "struct list {" C-examples/
C-examples/files/checkpoint/List.h:17:struct list {
C-examples/doublyLinkedLists.private/library/List.h:17:struct list {
C-examples/include/List.h:17:struct list {
C-examples/doublyLinkedLists/List.h:17:struct list {
C-examples/doublyLinkedLists/bad/List.h:17:struct list {
C-examples/doublyLinkedLists/library/List.h:17:struct list {
[amit@onyx examples]: