Go to the first, previous, next, last section, table of contents.
Here is an example shell command that invokes GNU @command{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. See section Regular Expressions. The `-i' option causes @command{grep} to ignore case, causing it to match the line `Hello, world!', which it would not otherwise match. See section Invoking @command{grep}, for more details about how to invoke @command{grep}.
Here are some common questions and answers about @command{grep} usage.
grep -l 'main' *.clists the names of all C files in the current directory whose contents mention `main'.
grep -r 'hello' /home/gigisearches for `hello' in all files under the directory `/home/gigi'. For more control of which files are searched, use @command{find}, @command{grep} and @command{xargs}. For example, the following command searches only C files:
find /home/gigi -name '*.c' -print | xargs grep 'hello' /dev/null
grep -e '--cut here--' *searches for all lines matching `--cut here--'. Without `-e', @command{grep} would attempt to parse `--cut here--' as a list of options.
grep -w 'hello' *searches only for instances of `hello' that are entire words; it does not match `Othello'. For more control, use `\<' and `\>' to match the start and end of words. For example:
grep 'hello\>' *searches only for words ending in `hello', so it matches the word `Othello'.
grep -C 2 'hello' *prints two lines of context around each matching line.
grep 'eli' /etc/passwd /dev/null
ps -ef | grep '[c]ron'If the pattern had been written without the square brackets, it would have matched not only the @command{ps} output line for @command{cron}, but also the @command{ps} output line for @command{grep}.
grep 'paul' /etc/motd | grep 'franc,ois'finds all lines that contain both `paul' and `franc,ois'.
cat /etc/passwd | grep 'alain' - /etc/motd
Go to the first, previous, next, last section, table of contents.