How to use grep to search for strings in files on the shell


  • 1.1 The GREP Command- An Overview
  • 1.2 The Basic grep Command Syntax
  • 1.3 How to Use the grep Command for Searching a File
  • 1.4 Recursive Use of grep
  • 1.5 Using grep to Search Only for Words
  • 1.6 Using grep to Search Two Different Words
  • 1.7 Count Line for Matched Words
  • 1.8 Grep Invert Match
  • 1.9 How to List Simply the Names of Matching Files
  • 1.10 More GREP command examples

1.1 The GREP command- an overview

The grep command, which means global regular expression print, remains amongst the most versatile commands in a Linux terminal environment. It happens to be an immensely powerful program that lends users the ability to sort input based on complex rules, thus rendering it a fairly popular link across numerous command chains. The grep command is primarily used to search text or search any given file for lines containing a match to the supplied words/strings. By default, grep displays the matching lines, and it may be used to search for lines of text matching one/many regular expressions in a fuss-fre, and it outputs only the matching lines.

1.2 The basic grep command syntax

The basic grep command syntax is as follows:

grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2'  filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName

1.3 How to use the grep command for searching in a file

In the first example, I will search for the user "tom" in the Linux passwd file. To search the /etc/passwd file for the user "tom", you need to enter the following command:

grep tom /etc/passwd

Given below are the sample Outputs:


You have the option to instruct grep to ignore word case, i.e., match abc, Abc, ABC and all possible combinations with the -i option ) as shown below:

grep -i "tom" /etc/passwd

1.4 Recursive use of grep

If you have a bunch of text files in a directory heirarchy, e.g, the apache configuration files in /etc/apache2/ and you want to find the file where a specific text is defined, then use the -r option of the grep command to do a recursive search meaning they can read all files under each directory for a string "" (as shown below):

grep -r "" /etc/apache2/

Alternatively, the following command may be used:

grep -R "" /etc/apache2/

Given below are the Sample outputs for a similar search on a nginx server:

grep -r "" /etc/nginx/
/etc/nginx/sites-available/ if ($http_host != "") {

Here, you would see result for on a distinct line preceded by the name of the file (for instance /etc/nginx/sites-available/ in which it was found. The inclusion of the file names in the output data may be easily suppressed by using the -h option (as explained below): grep -h -R "" /etc/nginx/ Given below are the Sample Outputs:

grep -r "" /etc/nginx/
if ($http_host != "") {

1.5 Using grep to search only for words

When you are searching for abc, grep will match all sorts of things, viz., kbcabc, abc123, aarfbc35 and lots more combinations. You can compel the grep command to select only those lines that contain matches to form whole words (those thatmatch only abc word), as shown below:

grep -w "abc" file

1.6 Using grep to search two different words

To search for two different words, you must use the egrep command as shown below:

egrep -w 'word1|word2' /path/to/file

1.7 Count line for matched words

The grep command has the ability to report the number of times a particular pattern has been matched for each file using the -c (count) option (as shown below):

grep -c 'word' /path/to/file

In addition, users may use the -n option preceding each output line with the number of the line in the text file from which it was obtained (as shown below):

grep -n 'root' /etc/passwd

Given below are the Sample outputs:


1.8 Grep invert match

Users may make use of the -v option to print inverts the match, which means it would match only those lines that do not contain the given word. For instance, print all lines that do not contain the word par by using the following command:

grep -v par /path/to/file

1.9 How to list only the names of matching files

You must use the -l option to list file name whose contents mention a particular word, for instance primary, using the following command:

grep -l 'primary' *.c

Lastly, you have the option to compel grep to display output in specific colors by using the following command:

grep --color root /etc/passwd

Given below are Sample Outputs:

1.10 More GREP command examples

In our second GREP command tutorial, you can find even more examples on how to use this Linux command.

Share this page:

Suggested articles

5 Comment(s)

Add comment


From: harshal sarode

hi sir,

i am facing a problm that how to fetch the common words from a file by grep.i tried thro the hardcoded string like

grep "abc" MyFile.txt.if i dont know if there are any common words in the file or not.then how i do it?plz help me so.


From: ramakrishna

Hi Sir,

I wants to get the content in beetween the particulers word like

It starts with subject and ends with subject and i wants the content in between that 


find . -exec grep -H "" {} \;



From: dkolb001

I have two files with content. file1 has two columns of content, and file2 has a single column of content.

I would like to use grep to find all matching rows of file1 content, and that matches file2 row of content and display.

Not all content has a match for file1 and file2, but I would like the match to be correct. 

Any advise for this?

From: Daniel

I'm a linux novice and im using the grep function to search for the number 1. "grep 1 tdocs" now its working it's giving me car1 wheel1 but then also car11 and 12. How do i go about specifing the search so it's just car1 and wheel1? Many Thanks.

From: Brandon

I need some help with grep...

I have two .txt files that contain lists, the first, a.txt, contains:


and the other, b.txt, contains


I want to use grep to output items from b.txt that arent in a.txt, in this case, "g"

I think it goes something like

grep -v --file=a.txt b.txt