Grep

From Linux 101, The beginner's guide to all things Linux.

Jump to: navigation, search

Grep is the incredibly powerful search tool on GNU and Linux based systems. It is easy to use for simple searches, and, thanks to its support for regular expressions, capable of very elaborate queries.

(Note that while grep is also present on other Unix systems like Solaris, these versions do not have all of the same features as the GNU version described here. See the man pages on your specific system for further details about specific differences.)

Contents

[edit] Introductory Example

Say, for example, that you want to find all instances of "hello" in a file called "hello.txt". From your command shell, this is as easy as executing the following:

grep hello hello.txt

You will immediately see any matches, including the line number on which they occur (by default). If you do not specify a filename to search in, then grep assumes you want to search the standard input. This makes it useful for searching on the output of another program:

ls -lh | grep -i pirate

The above example would find all lines containing "pirate" in the output of the ls command given. The -i option here tells grep to ignore the case. Ordinarily, grep queries are case sensitive, but in this case, "Pirate", "pirate", and "PiRaTE" would all be considered matches.

The general usage of grep is:

grep <what to search for> [in these files]

The option for what files to search for is optional. If you do not specify at least one, then grep will search from standard input.

[edit] Recursive Searching

Using the -r option and specifying a directory to search instead of a file allows for a recursive search on an entire directory.

grep -ri 'monkey business' /usr/src/linux

Would find all instances of the phrase "monkey business" within the linux source code. Note that the query here is encapsulated in single quotation marks. This is done whenever spaces or other special characters are used in the query, so that grep knows that "business" is part of the query and not the file or directory to search.

[edit] Regular Expressions

Regular expressions allow the use of wildcards, sets, unions, and other constructs, just like the regular expressions used in computer science theory courses.

Grep supports the use of "Basic" and "Extended" Regular Expressions, without actually needing to use the -E option for Extended expressions. (The -E option must be used for non-GNU grep)

For example:

ls | grep '[[:digit:]]'

This example will return all filenames that contain a number in their names, no matter where it appears.

Regular expressions are beyond the scope of this article, but this example should provide a glimpse of what is possible.

[edit] Conclusion

By now you should have an idea of how to use grep for some basic searches. The grep man pages provide even more information on how to use this powerful tool.

[edit] Usage Note

Grep is sometimes used as a verb by its more zealous fans, as an interchangeable replacement for words like "search" or "find." For example, "I can't grep my car keys!"

Personal tools