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/

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 
Foo
FOO
foo

Use Perl Regex

Perl-compatible 1 regular expressions are an extended set of regexes.

Example: Search 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:

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 donnt match a 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=:

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

Ignore file types

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

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

$ grep -r --exclude=*.log "criteria" /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" ./*
./file1.txt

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"

Show surrounding lines

To include surrounding lines in grep output, use -C N where N is the number of surrounding lines you want to show for each match.

$ grep -C 1 "bar" file.txt 
foo
bar
baz

Pipe grep

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":

$ 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