Grep usage examples

Last updated:
Table of Contents

All options for grep can be used either in a single-file mode (when you search in a single file) or in recursive mode (where grep searches for the pattern in all files under a directory.)

Search for string in a file

This is the simplest possible usage of grep. Lines containing the exact string "some text" passed as parameter will be output.

$ grep "some text" my_file.txt

Search for string in all files under directory, recursively

-r flag turns on recursive mode

This is the simplest possible usage of grep in recursive mode.

Use the -r flag and pass the directory as parameter, matches in any file under the passed directory (and all subdirectories, recursively) are output.

$ grep -r "some text" /path/to/directory/

Search for string in file, case insensitive

Search for all occurrences of string in target file, ignoring case.

$ grep -i "some text" my_file.txt

Search in file using Perl-compatible Regular expressions

To use recursive search, add -r modifier and pass a directory as argument instead

Perl-compatible 1

Search for lines that end in a word and a semicolon ';':

$ grep -P "\w+;$" my_file.txt

Search for a fixed, raw string

To use recursive search, add -r modifier and pass a directory as argument instead

(Useful for searching PHP or Perl code, for example, because they contain troublesome characters like '$' and '.').

Search for the actual string '$this->some_method().$this->another_method()':

$ grep -F '$this->some_method().$this->another_method()' my_file.txt

Wrap the string in double quotes if you need to match for a single quote:

$ grep -F "some ' string ('e'" my_file.txt

Search where lines don't match

To use recursive search, add -r modifier and pass a directory as argument instead

This is called inverted grep

Example: return all lines that don't include the string "some text"

$ grep -v "some text" my_file.txt

Search directory recursively, ignore directory

In this example, ignore .svn hidden directory.

$ grep -r --exclude-dir=.svn "criteria" /path/to/directory/

Search directory recursively, ignore multiple directories

In this example, ignore .svn, .git and runtime directories.

$ grep -r --exclude-dir={.svn,.git,runtime} "some text" /path/to/directory/

Search directory recursively, ignore file types

Search for "criteria" in all files under /path/to/directory/, except files ending in .log.

$ grep -r --exclude=*.log "some text" /path/to/directory/

Search directory recursively, display file names only

Use -l flag

Rather than the matching lines, the names of the files whose contents match the search criteria will be output.

$ grep -rl "some text" /path/to/directory/

Chain grep calls

You can chain grep calls to create pipelines:

For example: to return lines that don't match "STARTTLS=client|CMD|dhclient" and contain the string "Aug":

Note that the second call to grep has no -r modifier. -r is not used when you are piping other commands to grep

$ grep -rvP "STARTTLS=client|CMD|dhclient" ./ | grep "Aug"
Twitter Linkedin YC Hacker News Reddit

Show surrounding lines

Use -C N where N is the number of surrounding lines you want to show for each match.

$ grep -C 5 "some text" ./*

Pipe arbitrary output to grep (a.k.a. pipe grep)

Similarly to the example above where you pipe the output of a call to grep to another grep call (to further filter the results), you can pipe any output to grep.

For example, to filter the output of man vi for all lines that mention "vim", possibly prefixed by "g" (I've not included all output):

$ man vi | grep -iP "g?vim"

       vim [options] -t tag
       vim [options] -q [errorfile]
       gvim gview evim eview
       rvim rview rgvim rgview

See grep output in less with colours

Grep output is too large and you need to use a pager like less? You can still keep grep output with colours where patterns match!

Use --color=always and add -r to less:

$ man vi | grep --color=always -iP "g?vim" | less -r

output of grep in less You can pipe the output of grep to less and
keep formatting.


1: For more information on Perl-compatible regexes, see the official website for perl-compatible regular expressions

More info

Dialogue & Discussion