CentOS Debian Mint Ubuntu

15 examples of using Grep command in Linux

Examples of Using Grep Command in Linux

Grep, or Global Regular Expression Print, is an extremely useful Linux command for finding matching patterns. It can look through files and directories, and read input from commands as well. It searches with regex, or regular expressions and prints lines from a file that matches the given pattern.

In this post, we’ll be looking at how to use the Grep command in Linux. The commands discussed here have been tested on Ubuntu 20.04. The same Grep commands also work for Debian, Mint, Fedora, and CentOS.

Installing grep command

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

Grep command syntax

The basic syntax of the Grep command is as follows:

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

Using Grep Command

Finding a word in a file

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

$ grep <word> <filename>

For instance, the below command will look for the word “Donuts” in the file “Donuts.txt”.

$ grep Donuts Donuts.txt

Finding a word in multiple files

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

$ grep <word> <filename1> < filename2>

For instance, the below command will look for the word “Donuts” in both “Donuts.txt” and “cakes.txt”.

$ grep Donuts Donuts.txt cakes.txt

Ignoring case

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

$ grep donuts Donuts.txt

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

$ grep –i <word> <filename>

Finding a file by extension in a directory

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

Printing lines that do not match a pattern

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>

For instance, the following command will print all the lines except the one which contains the word “Donuts” in it.

Printing only the matching word

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>

For instance, the following command will print only the word “Donuts” instead of printing the entire line from the file “Donuts.txt”.

Printing line number of matching pattern and line

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>

For instance, the following command will print both the line number and the line that contains the word “Donuts”.

$ grep -n Donuts Donuts.txt

Printing file names that match a pattern

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” *

Printing the whole matching word

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

We searched for the string “do” and can see all the lines that contain “do”. 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

As you can see, it now only prints the line that contains the exact word “do”.

Matching lines that start with a particular word

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

Matching lines that end with a pattern

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

Matching lines that contain certain letters

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

Recursive Search

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

Printing specific lines before/after the matched words

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.

Printing 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 “hell” along with 1 line after it.

$ grep -A 1 hell Donuts.txt

Printing lines before the match

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 “hell” along with 1 line before it.

$ grep -B 1 hell Donuts.txt

Printing lines before and after the match

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 “hell” along with 1 line before and after it.

$ grep -C 1 hell Donuts.txt

Counting the number of matches

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

Note that our file “Donuts.txt” contains 4 “Donuts”, but -c has only printed the count as 3. It happens because it counts the number of lines instead of the number of matches.

For more information regarding grep, use the below command:

$ grep --help

or visit the man page using the below command:

$ man grep

This will take you to the Grep manual page.

That is all there is to it! In this post, we have explained the basic syntax and usage of the grep command in Linux. We also went through some command-line options to expand its usefulness. If you know some other uses of the Grep command in Linux that we have missed, we would love to know about them in the comments below.

Explore this related article to find out how you can find files and directories in Linux.

Similar Posts