1 Minute to Learn Grep Command in Linux with Examples

Updated: November 16, 2021

Grep command is one of the best common ones for Unix users. If you use it well, you can improve your productivity using Linux. In this article, I will help you learn grep command in linux with simple examples. I believe that you can be a grep expert after reading this post.

What is Grep command in Linux?

Grep is a command-line tool for looking for lines that match a regular expression in plain-text data sets. The text search pattern is called a regular expression. When it discovers a match, it generates lines contain the result to Terminal. It is an acronym that stands for Global Regular Expression Print. Nowadays, use can use grep without any installation on some Unix-like systems such as Ubuntu, Zorin OS, Fedora, Arch Linux and so on.

Learn Grep command with examples

I created a file named grep-linux.txt to help you learn grep command in Linux. You can create one and follow me.

THIS IS THE 1ST LINE IN THIS FILE.
This is the 2nd line in this file.

Phone: 0123456789
Email: [email protected]

1. Search for the string in a file

The grep command’s basic function is to look for a given string in a specified file, as demonstrated below. To search a string, you can use this command

grep "pattern" "file"

Searching a given string in files

The above command displays all instances that contain the word li in the file and outputs the results.

2. Grep with ignoring case-sensitivity

In the above example, I filtered the word li in the file, but it only return 2nd line because this line contains exactly line (lowercase). You can also grep with ignoring case sensitivity by following this command

grep -i "pattern" file

We use option -i or --ignore-case to filter words that ignore case sensitivity

Searching a given string in files that ignore case sensitivity

3. Finding whole words only

If you follow the first example to find a word that contains the word is, the result will be line 2 with three matches: This , is , this . To find whole words only, we will use option -w or --word-regexp

grep -w "pattern" file

This image will help you visualize it better

Difference between example one and example three

4. Grep with Regex

As we know, grep is stands for Global Regex Expression Print, so that you can use Regex to find patterns in files. Let consider this command to find a line that starts with character P (in Phone word)

grep regex_pattern file
grep ^P file

Grep in Linux with Regex

5. Counting the number of matches

Option -c stands for count. It helps you to display the number of lines where each line has letters matching the pattern. To use this grep command, you can use:

grep -c pattern file

Count of lines where it contains a match

In the first command, it returns 1 because there is only line 2 contains the is word. Same as in the 2nd statement, we have 2nd and 5th lines containing the word matching pattern.

6. Find files containing the extension

In Linux, the grep command also allows us to search for files that match the patterns, let’s look at the example of finding files with the extension webp.

ls Downloads | grep ".webp"

Find files have the extension webp

You can replace webp with any other extension to locate specific files. You can also use regex in the above grep command.

7. Grep all Files in Directory

To search files in the current directory (or any folder) that contain pattern, we can use an asterisk instead of a filename or location of the folder we want to grep. We can also use option -r to use recursive search.

grep -r pattern *
grep -r pattern ~/Downloads

Find all files in a folder

8. Find phone number by Grep command

You can use regex to filter phone numbers from text files. Note that you must adjust the template to fit the type of phone number you need.

grep '\(([0-9]\{3\})\|[0-9]\{3\}\)[ -]\?[0-9]\{3\}[ -]\?[0-9]\{4\}' file

Grep with Regex to find phone number

9. Using Grep to find email address

Same as finding phone numbers, you can also use grep command to find email addresses in Linux. To do it, you should run this command:

grep -E '\b[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b' file

Using Grep command in Linux to find email addresses

10. Show lines around result

When working with large files, having the option to select a search that displays the lines before, after, or around the result line can be useful. Using this grep’s option to do it:

grep -<A, B or C> <n> "pattern" file
-- A : after
-- B : before
-- C : around
-- n : count lines

Showing lines before, after, around line contains result

Conclusion

After reading this article, I hope you can know How to use the grep command in Linux. If you are needed, please leave a comment below, I will try my best to help you.