grep UNIX command allows you to find lines in files that contain key words or
phrases. With this command, it is possible to perform a quick search of a file or directory without having to look at
each file via a text editor or the UNIX
Simply put, grep searches an entire file for the pattern you want and displays its findings.
The basic syntax for
grep is as follows:
grep -cinv <pattern> <filename>
where the arguments next to the minus sign are options, <pattern> is the regular expression to search for, and
<filename> is the name of the file or files to search.
More information about
grep and the options available when using
grep, can be found by typing:
It is a good idea to use quotation marks, either single (‘ ‘), or double (” “) when defining the regular expression.
Not using these will cause unwanted side effects.
For example, if the following command was issued:
grep an myfile
grep would display every line with an occurrence of the pattern regardless of its position in a
word. “an”, “command”, “manual”, “and”, and “manage” all contain the regular expression “an”, and these lines would all be
displayed. This effect could cause some confusion. If, however, quotation marks are used as shown below, spaces could
be included to distinguish the pattern:
grep " an " myfile
Now, only the lines with the word “an” would be displayed.
Quotation marks also allow the use of special characters in the regular expression:
$match the end of a line
||match any single character except a newline|
||match zero or more of the preceding characters|
||match the beginning of a line|
||match one of the enclosed characters|
||match any character that is not enclosed|
Inside , you can use a minus sign (-) to indicate a range of single letters or digits. A backslash character (`),
also known as the backspace character, indicates that the following character should be taken literally.
The following command looks for a pattern that starts with a capital A, followed by any character, followed by zero or
more characters, and ending in a lowercase y in the file myfile. Lines that contained the following patterns would be
displayed (patterns are for examples only): “Ay”, “Amy”, “Aly”, “Ally”, “Allday”, etc.
To display lines with the pattern “12” in myfile, you can use:
grep " 1\2 "
To look for the pattern “hi” in the files, myfile1, myfile2, myfile3, and myfile4, you can use:
grep "hi" myfile
grep "hi" myfile[1-4]
For more information on searching for strings of text, please see the ITS documentation on wildcards.
Other Ways to Use Grep
Grep is a filter in UNIX. It can be very useful when used in conjunction
with pipes, the ‘|’ character (usually on the keyboard with the backslash).
For example, say you want to find out if the user “johndoe” is logged onto the system without scrolling through the
whole list of everyone currently logged on. Then you can pipe the results of the “who” command to a grep command
who | grep johndoe
If johndoe is logged on, the line containing johndoe from the results of the
command will be displayed on the screen. This way, only the information about johndoe is displayed.
Other Grep Commands
Other commands in the
grep family include
all support the options supported by
For more information, see the man pages for these commands.