Grep cheatsheet

Grep is the quintessential text search tool of the Unix shell. Many text search tools like ack and ag are popular now for searching in source code. But, for most common scenarios grep can still be a fast and good enough solution. It is available on every Linux machine you will be working at, so it is a huge bonus to be aware of its usage and capabilities.

  • Trivia: If you type :help grep in the Vim editor, you will see the origin of the name grep:
[Unix trivia: The name for the Unix "grep" command comes from ":g/re/p", where
"re" stands for Regular Expression.]
  • The most important use of grep is to search for text from standard input. This is usually done by piping output of another command to grep and searching in that. For example:
$ ls -1 | grep Xanadu

Note that the search text can be a regular expression. You can find more info about regular expressions from other detailed sources.

  • The other common use of grep is to search for text in files. For example:
$ grep Xanadu *.txt

Note that the *.txt is expanded by the shell and those filenames are passed to grep. Grep does not lookup the filenames.

  • To ignore case of the search text (make it case insensitive), use the -i option:
$ grep -i Xanadu *.txt
  • Since the filename lookup is actually done by the shell, it is not recursive by default. To search recursively in all files and directories starting from current directory:
$ grep -R Xanadu *

Note again, that the wildcard is expanded by the shell and passed to grep. Grep then takes each of those directories and it recurses through them on its own.

  • To exclude certain files, say save files, from grep use the --exclude option:
$ grep --exclude="*~" Xanadu *

To exclude multiple types of files, specify multiple --exclude options.

  • To exclude certain directories, say Git directories, from grep use the --exclude-dir option:
$ grep --exclude-dir=".git" Xanadu *
  • To exclude files based on glob patterns, you can also put those patterns in a file and pass it to the --exclude-from option. However, this did not seem to work for me.

  • By default, grep prints the file paths which have the search text. To print the line number, along with the file paths, use the --line-number option:

$ grep --line-number Xanadu *
  • Grep throws out a wall of text where the searched text can be hard to find. Asking it to use color using --color can be an useful option:
$ grep --color Xanadu *

On my computer, this shows the file path in purple, the line number in green and the searched text in red.

  • When searching text in source code files, it is helpful to get some context of where the text is found. Using the -C option you can ask grep to show you some lines of context around the result lines. For example, to get 3 lines of context above and 3 lines of context below every result line:
$ grep -C 3 Xanadu *.cpp
  • Your shell expands both matching filenames and directories and passes them as input to grep. When grep gets a directory as input, it warns that it Is a directory. For other files it might also give warnings such as No such device or address or Permission denied. To hide such messages use the --no-messages option:
$ grep --no-messages Xanadu *
  • Grep does look through binary files by default. If the searched text was found, it will just report that the binary file has that text, it does not try to print the matching line, for obvious reasons (no line separators in binary file). However, if you do not want grep poking around in binary files, use the -I or --binary-file=without-match option:
$ grep --binary-file=without-match Xanadu *

Tried with: Grep 2.25 and Ubuntu 16.04

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s