Grep - Tutorial
Grep - Tutorial
Linux grep commands FAQ: Can you share some Linux/Unix grep command examples?
Sure. The name grep means "general regular expression parser", but you can think of the grep command as a "search" command for Unix and Linux systems:
It's used to search for text strings and more-complicated regular expressions within one or more files.
I think it's easiest to learn how to use the grep command by showing examples, so let's dive right in.
Abridged examples
First up, if you dont like reading a bunch of text and just want to see a collection of grep commands, this section is for you. (If the Table of Contents over
there on the right side is still in the way, click or tap the hide link in its title to hide it):
case-insensitive
----------------
grep -i joe users.txt # find joe, Joe, JOe, JOE, etc.
regular expressions
-------------------
grep '^fred' /etc/passwd # find 'fred', but only at the start of a line
grep '[FG]oo' * # find Foo or Goo in all files in the current dir
grep '[0-9][0-9][0-9]' * # find all lines in all files in the current dir with three numbers in a row
1
show matching line numbers
--------------------------
grep -n we gettysburg-address.txt # show line numbers as well as the matching lines
grep in a pipeline
------------------
ps auxwww | grep httpd # all processes containing 'httpd'
ps auxwww | grep -i java # all processes containing 'java', ignoring case
ls -al | grep '^d' # list all dirs in the current dir
grep + find
-----------
find . -type f -exec grep -il 'foo' {} \; # print all filenames of files under current dir containing 'foo', case-
insensitive
2
recursive grep search
---------------------
grep -rl 'null' . # very similar to the previous find command; does a recursive search
grep -ril 'null' /home/al/sarah /var/www # search multiple dirs
egrep -ril 'aja|alvin' . # multiple patterns, recursive
(see http://alvinalexander.com/linux-unix/recursive-grep-r-searching-egrep-find)
That's the short version of the grep examples. The rest of this document describes many of these examples.
In a simple example like this, the quotes around the string fred aren't necessary, but they are needed if you're searching for a string that contains spaces, and
will also be needed when you get into using regular expressions (search patterns).
grep 'joe' *
The '*' wildcard matches all files in the current directory, and the grep output from this command will show both (a) the matching filename and (b) all lines in
all files that contain the string 'joe'.
3
As a quick note, instead of searching all file with the "*" wildcard, you can also use grep to search all files in the current directory that end in the file extension
.txt, like this:
This grep search example matches the string "score", whether it is uppercase (SCORE), lowercase (score), or any mix of the two (Score, SCore, etc.).
root 17937 0.0 0.0 14760 6880 ? Ss Apr01 0:39 /usr/local/apache/bin/httpd -k start
nobody 21538 0.0 0.0 24372 17108 ? S Apr03 0:01 /usr/local/apache/bin/httpd -k start
nobody 24481 0.0 0.0 14760 6396 ? S Apr03 0:00 /usr/local/apache/bin/httpd -k start
nobody 26089 0.0 0.0 24144 16876 ? S Apr03 0:01 /usr/local/apache/bin/httpd -k start
nobody 27842 0.0 0.0 24896 17636 ? S Apr03 0:00 /usr/local/apache/bin/httpd -k start
nobody 27843 0.0 0.0 24192 16936 ? S Apr03 0:00 /usr/local/apache/bin/httpd -k start
nobody 27911 0.0 0.0 23888 16648 ? S Apr03 0:01 /usr/local/apache/bin/httpd -k start
4
nobody 28280 0.0 0.0 24664 17256 ? S Apr03 0:00 /usr/local/apache/bin/httpd -k start
nobody 30404 0.0 0.0 24360 17112 ? S Apr03 0:00 /usr/local/apache/bin/httpd -k start
nobody 31895 0.0 0.0 14760 6296 ? S Apr03 0:00 /usr/local/apache/bin/httpd -k start
root 31939 0.0 0.0 1848 548 pts/0 R+ Apr03 0:00 grep http
(I deleted about half of the "httpd -k start" lines from that output manually to save a little space.)
Similarly, here's how you can find all the Java processes running on your system using the ps and grep commands in a Unix pipeline:
In this example I've piped the output of the ps auxwww command into my grep command. The grep command only prints the lines that have the string "java" in
them; all other lines from the ps command are not printed.
One way to find all the sub-directories in the current directory is to mix the Linux ls and grep commands together in a pipe, like this:
Here I'm using grep to list only those lines where the first character in the line is the letter d.
Using the Linux grep command to search for multiple patterns at one time (egrep)
You can use a different version of the grep command to search for multiple patterns at one time. To do this, just use the egrep command instead of grep, like
this:
This Unix egrep command searches the file named gettysburg-address.txt for the four strings shown (score, nation, liberty, and equal). It returns any lines from
the file that contain any of those words.
I should also note that "egrep" stands for "extended grep", and as you can see, it lets you do things like searching for multiple patterns at one time.
5
Searching for regular expressions (regex patterns) with grep
Of course the Linux grep command is much more powerful than this, and can handle very powerful regular expressions (regex patterns). In a simple example,
suppose you want to search for the strings "Foo" or "Goo" in all files in the current directory. That grep command would be:
grep '[FG]oo' *
If you want to search for a sequence of three integers with grep you might use a command like this:
grep '[0-9][0-9][0-9]' *
This next grep command searches for all occurrences of the text string fred within the /etc/passwd file, but also requires that the "f" in the name "fred" be in
the first column of each record (that's what the caret character tells grep). Using this more-advanced search, a user named "alfred" would not be matched,
because the letter "a" will be in the first column:
Regular expressions can get much, much more complicated (and powerful) than this, so I'll just leave it here for now.
This command doesn't show every line in every file that contains the string "StartInterval"; it just shows the names of all the files that contain this string, like
this:
com.apple.atrun.plist
com.apple.backupd-auto.plist
com.apple.dashboard.advisory.fetch.plist
com.apple.locationd.plist
org.amavis.amavisd_cleanup.plist
6
Of course you can also combine grep command arguments, so if you didn't happen to know how to capitalize "StartInterval" in that previous example, you
could just add the -i argument to ignore case, like this:
and that would have worked just fine as well, returning the same results as the previous grep command example.
grep -n we gettysburg-address.txt
Searching my sample gettysburg-address.txt file, I get the following output from this command:
grep before/after - Showing lines before or after your grep pattern match
After a recent comment, I just learned that you can display lines before or after your grep pattern match, which is also very cool. To display five lines before the
phrase "the living" in my sample document, use the -B argument, like this:
Similarly, to show the five lines after that same search phrase, use the -A argument with your Unix grep command, like this:
Of course you can use any number after the -A and -B options, I'm just using the number five here as an example.
This is a special way of mixing the Linux find and grep commands together to search every file in every subdirectory of my current location. It searches for the
string "foo" in every file below the current directory, in a case-insensitive manner. This find/grep command can be broken down like this:
Note that on Mac OS X systems you may be able to use the mdfind command instead of this find/grep combination command. The mdfind command is a
command-line equivalent of the Spotlight search functionality.
8
Related Unix/Linux grep commands and tutorials
We hope you enjoyed this Linux grep command tutorial and our grep examples.
There are at least two other commands related to grep that you should at least be aware of. The fgrep command stands for "fast grep", or "fixed strings",
depending on who you talk to. The egrep command stands for "extended grep", and lets you use even more powerful regular expressions.
The locate command is more related to the find command, but I thought I would note that it is good at finding files in the entire filesystem when you know
the filename, or part of the filename.
And as I mentioned in the previous section Mac OS X systems have the mdfind command. As a practical matter I use plain old grep 99% of the time.