How To Use grep Command In Linux / UNIX

How do I use grep command in Linux?

grep command searches the given file for lines containing a match to the given strings or words. By default, grep prints the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.

The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed:
g/re/p

grep command syntax

grep 'word' filename
grep 'string1 string2'  filename
cat otherfile | grep 'something'
command | grep 'something'

Use grep to search file

Search /etc/passwd for boo user:
$ grep boo /etc/passwd

You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option:
$ grep -i "boo" /etc/passwd

Use grep recursively

You can search recursively i.e. read all files under each directory for a string "192.168.1.5"
$ grep -r "192.168.1.5" /etc/

Use grep to search words only

When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word:
$ grep -w "boo" /path/to/file

Use grep to search 2 different words

use egrep as follows:
$ egrep -w 'word1|word2' /path/to/file

Count line when words has been matched

grep can report the number of times that the pattern has been matched for each file using -c (count) option:
$ grep -c 'word' /path/to/file
Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:
$ grep -n 'word' /path/to/file

Grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:
$ grep -v bar /path/to/file

UNIX / Linux pipes and grep command

grep command often used with pipes. For example print name of hard disk devices:
# dmesg | egrep '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i 'Model' /proc/cpuinfo

How do I list just the names of matching files?

Use the -l option to list file name whose contents mention main():
$ grep -l 'main' *.c
Finally, you can force grep to display output in colors:
$ grep --color vivek /etc/passwd

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 26 comments… read them below or add one }

1 Ivan 06.16.08 at 11:22 pm

I found this tutorial being the most clear and helpful one. Thank you.

2 manik sikka 11.25.08 at 12:26 am

This Tutorial is so far helpful to me.

Thank you..!!

3 turbolinux 12.12.08 at 11:46 am

what if i want to search keyword
like “$this->Products” or “['status']” ?

grep -R "['status']" 

doesnt work

4 Vivek Gite 12.12.08 at 11:53 am

Try

grep -R "\['status'\]"  filename
5 Arunkumar 12.26.08 at 5:32 am

this is very useful to me.. thanks …

6 Vikram 01.30.09 at 11:39 am

Very helpful. saved my time.

7 Domino 03.18.09 at 9:40 pm

What is the best way to grep recursively through a directory tree, and display the pattern matches, that occur in just all the *.cpp files?

For example:

grep -HRn “look for this” *.cpp

doesn’t work (on Linux)

8 eswar 03.23.09 at 9:02 am

what if i want to search like this

9 Tauqueer 03.25.09 at 8:13 am

Hi
I want to search t1.spq, t2.spq ….. tn.spq words from a file
grep -i “*.spq” filename doesn’t work
Please tell how can search such words??

-Regards,
tauqueer

10 eswar 03.25.09 at 9:45 am

avoid using * grep -i “spq” tt.sh
then you will get all the words which will have spq.
if some thing need to refrained from that need to get the desired out put then use

grep -i “spq” tt.sh | grep -v ” somepattern”

11 Humphrey 04.25.09 at 9:03 am

How can i grep for an output that come after a statement: eg Expires: 10/May/2009.

If i want to caputure only the date, how can i grep for what comes after the colon (:)

please help

12 Vivek Gite 04.25.09 at 10:21 am

Try,

echo 'Expires: 10/May/2009' | cut -d: -f2
OR
echo 'Expires: 10/May/2009' | awk -F':' '{ print $2}'

13 Rizvana 04.29.09 at 6:01 pm

This is quite informative…… Thanks.

Also I have a question, what is the expansion of ‘grep’? Can anyone answer?

14 Ravi 04.30.09 at 7:30 am

This helps a lot,,

15 Humphrey 05.02.09 at 12:11 am

Rizvana,
grep means Get Regular Expression and Print

16 Humphrey 05.02.09 at 8:44 pm

How can i do calculation on dates;
eg to know the number of days between ‘todays date’ and a day like ‘15/may/2009′

please help

17 divya 05.04.09 at 2:24 pm

thanks .i gt the rite information.

18 Blury 05.11.09 at 3:34 am

Hi,
How can I use grep function to search files that file name with “ord” or “rec” from specific dir??

19 kandida 06.02.09 at 7:04 am

Hi
lets say i have some data :
a
a
a
b
b
b
c
c
c
can I use grep comand to make like this :
a
b
c
thanks

20 Vivek Gite 06.02.09 at 8:04 am

Try uniq command.

21 kandida 06.02.09 at 9:52 am

Vivek,
its working…..thanks a lot

22 Blury 06.03.09 at 12:51 am

Hi,
No one know how to use grep function to search files that file name with “ord” or “rec” from specific dir??

23 Vivek Gite 06.03.09 at 1:03 am

Try

cd /dir/to/search
grep "word" ord*
grep -R "word" rec*
24 snake 06.10.09 at 3:46 pm

Hi,
I’d like to get the total cpu and memory usage easily and I think of using ‘dstat’ command. Can I get the values corresponding to the free and used column with grep?

------memory-usage-----
used buff cach free
153M 876k 24M 4392k

cheers!

25 Nazeem S 06.22.09 at 10:34 am

The Most Helpful POST

26 Thomas K 06.26.09 at 1:36 pm

For those who want to search files with wild cards and the like, try the find command with -exec. find /dir/to/search/ -iname *.cpp -exec grep 'word' '{}' \;
and snake, I do not think it is possible to search columns with grep, I’m 98% sure that it is line (row) only.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , , , , , , , , , , ,

Previous post: Linux: Openssh (ssh server) deny root user access

Next post: FreeBSD install Perl language