17 examples of using Grep command in Linux

Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. The text search pattern is called a regular expression. When it finds a match, it prints the line with the result. The grep command is handy when searching through large log files.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux related Software Installation tasks.

In this context, we shall look into how to use the Grep command in Linux.


How to install grep command on Ubuntu ?

Most Linux distributions including Ubuntu 20.04 LTS come with grep installed. In case your system does not have Grep installed, you can install it using the below command in Terminal:

$ Sudo apt-get install grep


What is Grep command syntax ?

The basic syntax of the Grep command is as follows:

$ grep [options] PATTERN [FILE...]


How to use Grep Command ?


1. Using Grep command to find a word in a file

With Grep, you can search any word in a specific file. The syntax would be:

$ grep <word> <filename>


2. Using Grep command to find a word in multiple files

You can also search for a specific word in multiple files.

$ grep <word> <filename1> < filename2>


3. Ignoring case using Grep

Grep performs case-sensitive searches by default, this means that it treats "Phone" and "phone" differently. If we again run the above command with "phone", you will see no output this time.

$ grep phone Phone.txt

To ignore the case, you will need to add "-i" to your command like this:

$ grep –i <word> <filename>


4. How to find a file by extension in a directory using Grep?

You can also find a file in a specific directory by its extension. Use the following syntax to do so:

$ ls ~/<directory> | grep .<file type>

For instance, to find all “.jpeg” files in the “Downloads” folder, the command would be:

$ ls ~/Downloads | grep .jpeg


5. How to Print lines that do not match a pattern with Grep command ?

With Grep, you can also print the lines that don’t match your pattern by adding “-v” to your command. 

Use the following syntax to do so:

$ grep -v <word> <filename>


6. How to Print only the matching word with Grep command ?

By default, grep prints the whole line that contains the pattern. To print just the matching word, add "-o" to your command.

$ grep -o <word> <filename>


7. How to Print line number of matching pattern and line with Grep ?

By default, grep prints the entire line that contains the pattern. To print both the line number and line that contains the matching word, add “-n” to your command.

$ grep -n <word> <filename>


8. How to Print file names that match a pattern using Grep Command ?

To print all the files in your current directory that match your pattern, use the following syntax:

$ grep -l "<word>" *

For instance, the following command will print all the files that contain the word “Hello”:

$ grep -l “Hello” *


9. How to Print the whole matching word with Grep ?

By default, grep prints all the words that match your pattern, even if they are part of a word.

The Syntax is:

grep 'word' filename

To match the exact word, add “-w” to your command:

$ grep –w <word> <filename>

For instance, to search the exact word “do” in the Donuts.txt file, the command would be:

$ grep -w do Donuts.txt

This will print the line that contains the exact word “do”.


10. How to match lines that start with a particular word with Grep?

To print lines that start with a particular word, add “^” to your command.

$ grep ^<word> <filename>

For instance, the following command will print all the lines that start with “Th”.

$ grep ^Th Donuts.txt


11. How to match lines that end with a pattern using Grep Command ?

To print lines that end with a particular pattern, add “$” to your command.

$ grep <word>$ <filename>

For instance, the following command will print all the lines that end with “t”.

$ grep t$ Donuts.txt


12. How to match lines that contain certain letters using Grep command ?

If you want to look for lines that have one or multiple letters in them, use the following syntax:

$ grep “[<letters>]” <filename>

For instance, the following command print all the lines that contain any letter from “a” to “f”.

$ grep “[a-f]” Donuts.txt


13. How to make a Recursive Search using Grep command ?

To look up all the files in a directory and sub-directory for a particular pattern or word, use:

$ grep -R <word> <path>

For instance, the following command will list all the files that contain the word “Hello” in the Documents directory. 

In the below output, you can see the file paths along with the matching words.

$ grep -R “Hello” /home/maryam/Documents


14. How to Print specific lines before/after the matched words using Grep ?

By default, Grep prints only the line that matched the specific pattern. With -A option in Grep, you can also include some lines before or after the matched words.


To print lines after the match:

To print the lines that contain the matched words including N number of lines after the matches:

$ grep -A <N> <word> <filename>

For instance, the following command will print the line containing the word “fruits” along with 1 line after it.

$ grep -A 1 fruits Donuts.txt


15.  How to Print lines before the match using Grep ?

To print the lines that contain the matched words including N number of lines before the matches:

$ grep -B <N> <word> <filename>

For instance, the following command will print the line containing the word “fruits” along with 1 line before it.

$ grep -B 1 hell Donuts.txt


16. How to Print Lines before and after the match using Grep ?

To print the lines that contain the matched words including N number of lines before and after the matches:

$ grep -C <N> <word> <filename>

For instance, the following command will print the line containing the word “fruits” along with 1 line before and after it.

$ grep -C 1 fruits Donuts.txt


17. How to count the number of matches using Grep command ?

Using the -c option with grep, you can count the lines that match a specific pattern. Remember, it will only match the number of lines not the number of matched words.

$ grep -c <word> <filename>

For instance, the following command prints the count of lines that contains the word “Donuts”.

$ grep -c Donuts Donuts.txt

For more information regarding grep, use the below command:

$ grep --help

or visit the man page using the below command:

$ man grep

This command will display the Grep manual page.


[Need urgent assistance to fix Linux related Installation issues ? We are available to help you. ]

This article will guide you on the basic syntax and usage of the #grep #command in #Linux. We also went through some command-line options to expand its usefulness. The easiest of the two #commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. 

Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.

1. grep searches one or more input #files for lines that match a given pattern and writes each matching line to standard output. 

2. If no files are specified, grep reads from the standard input, which is usually the output of another command.

Related Posts