How to use grep command in Linux

In one of the previous articles we discussed find command in linux and how to use it. Likewise in this article also we are going to discuss another linux command which is useful to search something in Linux. That is “grep” command. We can search text patterns in the files using grep and on the other side we can use find command to search for files in linux OS. More than that we can filter search results to capture a specific string of text, a word or a number using grep command. Remember this command is very useful for our daily tasks in Linux Operating system.

Simply what does grep command do ? It searches for lines which matches with the given text to find inside the given files you mention with the command. The output is a set of lines and each line should have the specific word or word phrase you typed with the grep command.

So let’s begin the discussion.

Syntax – 

  • grep <word> <file name>
  • grep <word phrase> <file name>
  • grep <word> <file name1> <file name2> <file name n>
  • grep <options> <word> <file name>
  • cat <file name> | grep <word>

Important – The file name can be absolute path or relative path. You can use relative path if you search a text inside of a file in your location. But if you are searching a text in a file in another location, you have to use the absolute path. 

When using the word phrase remember to use ” ” or ‘ ‘. 

You can give one or more file names as mentioned in above syntaxes. 

grep command with examples

1) Search your username in the /etc/passwd file

grep ubuntu /etc/passwd

Output
 

how to use grep command in linux

As mentioned in the syntax, we can use this grep in below method also. First we view the file using cat command and then grep that output for our keyword. 

how to use grep command in linux

Both ways are correct.

2) Search a keyword  in more than one file

grep ubuntu /etc/passwd /etc/groups

Output

how to use grep command in linux



On the above image, you can see we have typed the command to search the keyword “ubuntu” ( our username ) in two files. They are /etc/passwd file and /etc/group file. So the output is as above.
We can see clearly the output has three sections with three colors. Purple, white and red color.

– Purple : the file name
– White : the line which containing our keyword ( ubuntu in this time )
– Red : Our keyword

So, normally grep command gives the output like that when we are searching in more than one file. We can change this output as we need. We have to use Options to do that.

grep command with options

Option 1 : Use -n to get the line number of the file

how to use grep command in linux

Option 2 : Use -h to hide the file name from the output

how to use grep command in linux

Option 3 : Use -l to list only the files which containing our keyword. 

how to use grep command in linux

Actually above option ( -l ) is very useful when we are searching a keyword in the whole file system.


Before talking about -l option briefly i am moving to another option.

Option 4 : Use -R to search a keyword recursively. 

how to use grep command in linux



On the above example, i am searching my name osanda under root directory. If we used the command without option -R, below will be the output.

how to use grep command in linux

Important : Grep command always searches keyword in files, not in directories. 

Since / is the root directory, we need to give -R option to search in all the files in sub directories.

Important : 2>/dev/null is performing sending the errors in the output to a garbage bin. 

Let’s come back to Option 3, which is -l option.

how to use grep command in linux


 As on above image, we can limit the output only to view the file names which contains the keyword “osanda”.

Important : You can use both -R or -r for recursive grep. 

Option 5 : Use -i to ignore case sensitivity

here, i have created a small text file named “osa”. It includes two words on the same line as below.

See below how -i option works


3) Search a word phrase in a file

Option 6 : Use -c to count the number of results in a file

Above example counts number of lines having error in /var/log/syslog file.

Option 7 : Use -w to search exact keyword

Think you are searching a word named boo. Guess we have a file named example.txt. Inside of that file, it is having lines with exactly boo. some lines may be having fooboo, fooboob, booboo, blackboo. So if we typed the grep command as noramlly ( grep boo example.txt ) we get all these lines in our output. But we don’t need them. We only need the lines containing just boo. So the command should be like below

grep -w boo example.txt

How to search two words in a single file

grep -w ‘word1|word2’ example.txt

Option 8 : Use -v option to ignore a keyword from the search results

On the above example, i have ignored keyword “ubuntu” from the output. So except ubuntu, it lists the lines from /etc/passwd file.

Some examples how can we use grep with other commands.

Option 9 : Use –exclude-dir to ignore a directory when searching

Guess we need to ignore a directory to search. You need to mention the directory name with the mentioned option. 
eg – grep -Ri “index.html” –exclude-dir blog
here blog is the directory we need to skip when searching. 
 
Important – we can use grep command not only to search string pattern in files, But also we can filter specific strings patterns from the the different command outputs. 

1) Display all disk details

 
2) Check the errors in syslog file


3) Get the mysql-server package from package list results. 


4) Check processes running for a specific service. 

Guess you need to check if memached processes are running or not. Whatever service you need to check. Normally we are using ps aux command to check the processes. But it outputs all the processes currently running. But we need to check only memcached processes. We can use grep command with pipeline as below.

ps aux | grep memcached

Think if we need to find the ports allocated for memcached service. Then you should try like below

netstat -plunt | grep memcached


So in this article, you have learned grep commands with mostly usefull options. You can read the grep command manual with man grep. Read more and more and do practices.

Uselful resources

Cheers…!

Leave a Reply