Node: Usage,  Next: Reporting Bugs,  Prev: Regular Expressions,  Up: Top

4 Usage
*******

Here is an example command that invokes GNU `grep':

     grep -i 'hello.*world' menu.h main.c

This lists all lines in the files `menu.h' and `main.c' that contain
the string `hello' followed by the string `world'; this is because `.*'
matches zero or more characters within a line.  *Note Regular
Expressions::.  The `-i' option causes `grep' to ignore case, causing
it to match the line `Hello, world!', which it would not otherwise
match.  *Note Invoking::, for more details about how to invoke `grep'.

   Here are some common questions and answers about `grep' usage.

  1. How can I list just the names of matching files?

          grep -l 'main' *.c

     lists the names of all C files in the current directory whose
     contents mention `main'.

  2. How do I search directories recursively?

          grep -r 'hello' /home/gigi

     searches for `hello' in all files under the `/home/gigi' directory.
     For more control over which files are searched, use `find',
     `grep', and `xargs'.  For example, the following command searches
     only C files:

          find /home/gigi -name '*.c' -print0 | xargs -0r grep -H 'hello'

     This differs from the command:

          grep -rH 'hello' *.c

     which merely looks for `hello' in all files in the current
     directory whose names end in `.c'.  Here the `-r' is probably
     unnecessary, as recursion occurs only in the unlikely event that
     one of `.c' files is a directory.  The `find ...' command line
     above is more similar to the command:

          grep -rH --include='*.c' 'hello' /home/gigi

  3. What if a pattern has a leading `-'?

          grep -e '--cut here--' *

     searches for all lines matching `--cut here--'.  Without `-e',
     `grep' would attempt to parse `--cut here--' as a list of options.

  4. Suppose I want to search for a whole word, not a part of a word?

          grep -w 'hello' *

     searches only for instances of `hello' that are 