Press "Enter" to skip to content

10 Practical Examples of the Linux Grep Command

The grep command provides access to the grep utility, a powerful file processing tool used to find patterns in text files. It has many practical use cases and is certainly one of the most used Linux commands. This guide illustrates some simple yet useful Linux grep commands that have real-world uses.

Example File for Demonstration

We have created a reference file to help readers understand grep more effectively. You can create a copy of this file by issuing the following shell command in your terminal.

cat <<END >> test-file
This is a simple text file that contains
multiple strings as well as some telephone numbers
(555) 555-1234 (567) 666-2345
and email plus web addresses
john@doe.com
https://google.com
ftp://mywebserver.com
END

1. Find Text in Files

To search for text pattern in a file, simply run grep followed by the pattern name. Also, specify the name of the file that contains the text.

grep "email" test-file

This command will display the line in our test-file that contains the word email. You can also search the same text in multiple files using grep.

grep "example" /usr/share/dict/american-english /usr/share/dict/british-english

The above command displays all instances of the word example in the american-english and british-english dictionary files.

2. Find Exact Match Words

The Linux grep command illustrated in the earlier example also lists lines with partial matches. Use the below-given command if you only need the exact occurrences of a word.

grep -w "string" test-file

The -w or –word-regexp option of grep limits the output to exact matches only. Grep consists of some additional flags that can be used with the default command as well.

3. Ignore Case Distinctions

By default, grep searches for patterns in a case-sensitive way. However, you may want to turn this off if you don’t know in what case the pattern is beforehand.

grep -i "this" test-file

Use the -i or –ignore-case option for turning off case sensitivity.

4. Count the Number of Patterns

The -c flag stands for count. It displays the number of matches that were found for a particular pattern. Admins can use this for retrieving specific information about the system.

You can pipe the ps command with grep to count the processes that belong to the current user.

ps -ef | grep -c $USER

The following command displays the number of MP3 files present in a directory.

ls ~/Music | grep -c .mp3

5. Display Line Numbers Containing Matches

You may want to find the line numbers that contain a certain match. Use the -n or –line-number option of grep to achieve this.

cat /etc/passwd | grep -n rubaiat

This option is particularly useful for debugging source codes and troubleshooting log files. To display all the numbers for lines in the ~/.vimrc that are used for configuring the Vim text editor:

grep -n "set" ~/.vimrc

6. Find Filenames Using Extensions

To get a list of all the MP3 files present in the ~/Music directory:

ls ~/Music/ | grep ".mp3"

You can replace .mp3 with any other extensions for locating specific files. The following command lists all the php files present in the current working directory.

ls | grep ".php"

7. Find Patterns in Compressed Files

Linux grep command can also find patterns inside compressed files. You will need to use the zgrep command for doing this, however. First, create a compressed archive of our test-file by typing:

gzip test-file

Now, you can search for text or other patterns inside the resulting archive.

zgrep email test-file.gz

8. Find Email Addresses

Admins can also list email addresses from text files using the Linux grep command. The following example does this by searching for a regular expression pattern.

grep '^[a-zA-Z0-9]\+@[a-zA-Z0-9]\+\.[a-z]\{2,\}' test-file

You can find regular expressions for doing similar jobs or you can create them yourself if you know how they work.

9. Find Phone Numbers Using Grep

You can use grep regular expressions for filtering out phone numbers from a text file. Note that you’d have to tweak the pattern to match the type of phone numbers you need.

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

The aforementioned command filters out ten-digit American telephone numbers.

10. Find URLs From Source Files

We can leverage the power of grep for listing out URLs found in text files. The below-given command prints all the URLs present in the test-file.

grep -E "^(http|https|ftp):[\/]{2}([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4})" test-file

We’re again using the -E option for extended regular expressions. You can also use the egrep command to avoid adding this.

egrep "^(http|https|ftp):[\/]{2}([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4})" test-file

Mastering the Linux Grep Command

We’ve presented several useful examples of the Linux grep command for tackling real-world problems. Although these examples illustrate the power of grep for text processing, you’ll need to master regular expressions if you want to be super productive with grep.

Sometimes Linux users bump into certain situations where they can’t remember the various options related to a command. Hopefully, the Linux operating system provides you with ways to get command-line help for almost every system utility.