Grep Command
Grep Command
grep is a powerful file pattern searcher that comes equipped on every distribution
of Linux. If, for whatever reason, it is not installed on your system, you can easily install
it via your package manager (apt-
get on Debian/Ubuntu and yum on RHEL/CentOS/Fedora).
1. Search and Find Files
# dpkg -l | grep -i python
First, we ran dpkg –l, which lists installed *.deb packages on your system. Second, we
piped that output to grep –i python, which simple states “go to grep and filter out and
return everything with ‘python’ in it.” The –i option is there to ignore-case, as grep is
case-sensitive. Using the –i option is a good habit of getting into, unless of course you are
trying to nail down a more specific search.
2. Search and Filter Files
# grep –v “#” /etc/apache2/sites-available/default-ssl
The –v option tells grep to invert its output, meaning that instead of printing matching
lines, do the opposite and print all of the lines that don’t match the expression, in this
case, the # commented lines.
3. Find all .mp3 Files Only
# find . –name “*.mp3” | grep –i JayZ | grep –vi “remix”
he grep can be very useful for filtering from stdout. For example, let’s say that you have
an entire folder full of music files in a bunch of different formats. You want to find all of
the *.mp3 files from the artist JayZ, but you don’t want any of the remixed tracks. Using
a find command with a couple of grep pipes will do the trick:
In this example, we are using find to print all of the files with a *.mp3 extension, piping
it to grep –i to filter out and prints all files with the name “JayZ” and then another pipe
to grep –vi which filters out and does not print all filenames with the string (in any case)
“remix”.
4. Display Number of Lines Before or After
Search String
# ifconfig | grep –A 4 eth0
# ifconfig | grep -B 2 UP
Another couple of options are the –A and –B switches, which displays the matched line
and number of lines either that come before or after the search string. While the man page
gives a more detailed explanation, I find it easiest to remember the options as –A = after,
and –B = before:
5. Prints Number of Lines Around Match
# ifconfig | grep –C 2 lo
The grep’s –C option is similar, but instead of printing the lines that come either before
or after the string, it prints the lines in either direction:
6. Count Number of Matches
# ifconfig | grep –c inet6
Similar to piping a grep string to word count (wc program) grep’s built-in option can
perform the same for you:
7. Search Files by Given String
# grep –n “main” setup..py
The –n option for grep is very useful when debugging files during compile errors. It
displays the line number in the file of the given search string:
8. Search a string Recursively in all Directories
# grep –r “function” *
If you would like to search for a string in the current directory along with all of the
subdirectories, you can specify the –r option to search recursively:
9. Searches for the entire pattern
Passing the –w option to grep searches for the entire pattern that is in the string. For
example, using:
# grep –E
This is just a starting point with grep, but as you are probably able to see, it is invaluable
for a variety of purposes. Aside from the simple one line commands we have
implemented, grep can be used to write powerful cron jobs, and robust shell scripts, for
a start.