20 Powerful Ways To Use Grep Command

Practical Grep Command Examples In Linux UNIX Ubuntu and Mac OS X Bash Shell

Grep is a popular unix command line utility on unix operating systems. It stands for Global Regular Expression Print. Knowing grep command can help you on any unix based system, this includes Mac OS X, Ubuntu and linux based systems.

As a software developer, I find these commands really handy for debugging and quick search on various systems. Most of server side application software runs on a remote machine where you have limited access. These commands can give you a lot of strength if you have to simple log file analysis or a quick code change.

This tutorials is focused on bash shell, however most of command should also work on other popular shells. If you are intesrsted in learning in-depth linux/unix operating system concepts you must check out these books.

Basic GREP Command Usage

Basic grep command is super easy to use. e.g.  you can just find text “blah" in a file FROMDEV.txt with below command

grep “blah” FROMDEV.txt

Below mentioned options will give you more advanced usage of grep command and text search in general on unix bash shell.

Case Insensitive Search In A File

Regular grep command is case sensitive therefore you need to use -i option to do case insensitive text search.
grep -i “blah” FROMDEV.txt

Find Exact Matching Words

Grep will find any text containing the text. To avoid this you can use -w option to match exact word.

grep -w “blah” FROMDEV.txt

this can also be done case-insensitive by adding -i option.

grep -wi “blah” FROMDEV.txt

You can also search for more than one word in same command, just separate the words by escaped pipe operator ( backslash followed by pipe) . e.g. below command will search for “blah” or “plop” in FROMDEV.txt file

grep -wi “blah\|plop” FROMDEV.txt

Colorizing The Grep Output

You can use GREP_OPTIONS env variable to set colors on grep results.

export GREP_OPTIONS='--color=auto'

Run the above  command in terminal before running grep command and you will see results of grep will be color coded. You can also include this line in $HOME/.bash_profile file to make it as your default setting.

Print Lines Before And After

This is how you can display lines before and after the text you are searching

You can use grep -A , -B and -C options

  • A = lines after
  • B = lines before
  • C = lines before and after.

The below command will find text “blah” in a file FROMDEV.txt including 5 lines before and 7 lines after.

grep -B5 -A7 blah FROMDEV.txt

To find 5 lines before and after use below command

grep -C5 blah FROMDEV.txt

Note that there is no gap between the options (A/B/C) and number

Search For Committed Code In Git History

You may want to search an old git commit based on a change you made in a file. The below command will search in all previous git commits to find the content.

git rev-list --all | xargs git grep "blah"

Find Patterns Across Multiple Lines

You can use pcrgrep command to do this with -M option. Below example will search “ONE” followed by “TWO” in multiple lines

pcregrep -M  ‘ONE.*(\n|.)*TWO' FROMDEV.txt

Find all files containing a text recursively in all directories and subdirectories

You can do it multiple different ways. My favorite choice is find + grep

Below command will find all the java files containing “StringBuffer” text in it. I use it a lot to catch performance issues related to usage of StringBuffer class in java.

find . -name "*.java" | xargs grep "StringBuffer"

Second options is grep command with -r option

grep -rn . -e "StringBuffer"


grep -Rn . -e "StringBuffer"

But the above command may search all files, therefore you can use below command to search only specific files. e.g.   searching this in only java files.

grep "StringBuffer" -r --include=*.java  .


grep "StringBuffer” -R --include=*.java  .

Dot is used to run on current directory, you can always replace it with any path in your system.

Find Number Greater Than 13 In A File

This requires use of AWK command. Below command can do number comparison and print values.

cat FROMDEV.txt | awk '{if ($1>=13) print $1}'

You can change the same command to do other things, e.g. find numbers less than 14

cat FROMDEV.txt | awk '{if($1<14) print $1}'

Show Only Words That Match The Pattern In The File

This can be achieved by -o option

grep -oh blah FROMDEV.txt

Find Something In A Constantly Changing File

Or Look For Something In A Running Log File

This is a common scenario where you want to monitor a server log for some exception.  You can do this using pipe and tail command

tail -f server.log  | grep Exception

How To Look For Past Git Commits With A Specific Text

I use this to find an old commit message from git relating to a specific BUGID in commit message

git log | grep -b5 BUGID

Find Text With A Quote In A File

Single quote and double quote both have special meanings in regular expressions. Therefore you need to escape them using back slash.  Following command will search for quoted “blah” text

grep “\”blah\”” FROMDEV.txt

and following command will search for quoted ‘blah' text

grep “\’blah\'” FROMDEV.txt

Exclude Some Patterns In List Of Results

You can do this by using grep -v option. e.g. below option shows you all Exception in log file except IOException

grep “Exception” server.log | grep -v “IOException”

Find And Replace A String In Files Recursively

Below command can replace all occurrences of StringBuffer with StringBuilder in all java files.

find . -name “*.java" | xargs sed -i -- ’s/StringBuffer/StringBuilder/g'

To do this on single file you can do only with sed command like this

sed -i -- ’s/StringBuffer/StringBuilder/g’ Test.java

Count The Number Of Results In Grep Command

To count all occurrences of matching string you can use grep -c

grep -c “blah” FREOMDEV.txt

if you want to count the number of lines that have the searched word (at least one occurrence) than you can use WC command with grep

grep “blah” FROMDEV.txt | wc -l

Show Only File Names That Contain A Searched Text

Below command should display all files in current directory that have text “blah”

grep -l “blah” *

You can make it recursive by adding -r option to search all files in subdirectories

grep -rl “blah” *


grep -Rl “blah” *

Show Line Number In Output Of Grep

grep -n “blah” FROMDEV.txt

Count Empty Lines In A File

grep -c "^$” FROMDEV.txt

Search For More Than One String In Same Line Of File

You may want to find the lines that have two different text in the line. This can be achieved by running multiple grep commands with pipe operator. e.g. below command will find all the lines that contain text “blah” and “plop” in same line.

grep “blah” FROMDEV.txt | grep “plop”

Search For All The Files That Are Recently Modified

This is not a grep command, however I could not resist it adding it here since it may be handy to do search using grep. Lets say if you want to find out all java files that have been modified in last 5 days. Below command will find you all files.

find . -name "*.java" -mtime -5

Now you can pipe a grep command to look for a specific string in these files. e.g. Find all java files that are modified in last 3 days and have StringBuffer used in it.

find . -name "*.java" -mtime -3 | xargs grep “StringBuffer”

Did you find this tutorial useful? Please share it with your friends.


Ubuntu 662638113177399604

Post a Comment Default Comments