Grep usage examples

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 in file

Lines containing the exact string "some text" in file my_file.txt will be output:

$ grep "some text" my_file.txt

Search in files under directory

-r flag turns on recursive mode

Search for string in all files under a given directory, recursively.

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/

Case insensitive

Add modifier -i to return matches in a case-insensitive way.

Example Search for all occurrences of "foo" in target file, ignoring case.

$ grep -i foo file.txt 

Perl Regex

Perl-compatible1 regular expressions are an extended set of regexes.

Example: Search my_file.txt for lines that end in a word followed by semicolon ';':

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

Search for raw string

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

Use -F modifier and wrap the string in quotes (" or '):

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

    $ grep -F '$this->some_method().$this->another_method()' my_file.php
  • Example Use double quotes to match the string "don't":

    $ grep -F "some 'don't" my_file.txt

Inverted grep

To search for lines that don't match some criteria, use the -v modifier:

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

$ grep -v "some text" my_file.txt

Ignore directory

Use --exclude-dir={some-dirname} to make grep ignore a directory when looking for matches.

Example: search files under /path/to/directory/ for "criteria", except for files under .svn directory

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

Ignore multiple directories

To make grep ignore multiple directories, you can pass multiple names for --exclude-dir=:

Example: search for "some text", except under .svn and .git directories

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

Ignore file types

Use --exclude=<pattern> to remove specific file types from the output:

Example: Search for "some text" in all files under /path/to/directory/, except files ending in .log.

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

Display file names only

Add the -l flag to show the file names where there are matches instead of the text itself:

$ grep -l "some text" ./*

Chain grep calls

You can chain grep calls to create pipelines:

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

Example: lines that don't match "foo" and contain the string "bar":

$ grep -rvP "foo" ./ | grep "bar"

Show surrounding lines

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

Example: search for "bar", show one surrounding line (above and below)

$ grep -C 1 "bar" file.txt 

Pipe grep

You can pipe any output to grep.

Example search the output of man vi for all lines that mention "vim", possibly prefixed by "g":

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

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

View in less, keep colours

You can view the output of grep in a pager like less without losing the colours:

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

Dialogue & Discussion