12 Practical Grep Command Examples In Linux

sysxploresysxplore
6 min read

In the Linux command line, the grep command is a powerful tool for searching text within files.

In this article, I will go over 12 examples of grep command usage that every Linux user, sysadmin, and developer should be aware of.

What is the Grep command in Linux?

If you're wondering what grep stands for, it stands for global regular expression print. You can use grep to search through files or use it in conjunction with pipes or other commands to filter the output of another command. You'll find the grep command very useful in your day-to-day work as a sysadmin or Linux user once you've mastered it.

The syntax of the grep command is very simple:

$ grep [OPTION...] PATTERNS [FILE...]

In this article, I will use a file called 'linuxquotes.txt' to demonstrate how to use the commands. If you want to follow along you can find the contents of the file here :

1-grep-file-contents.png

Grep command examples

Now that you know what grep is, let's look at some examples.

1. Finding all occurrences of a string in the given file.

This example, we tell grep to look for the word "Linux" and provide the file to look into as an arguement. If the string you want to search contains spaces, you must surround it with quotes:

┌──(traw㉿kali)-[~/articles]
└─$ grep "Linux" linuxqoutes.txt

2-example-1.png

2. Finding all occurrences of a string in in multiple files.

We can also instruct grep to search multiple files for a specific string. The following example demonstrates this clearly:

┌──(traw㉿kali)-[~/articles]
└─$ grep "Linux" linuxqoutes.txt learnlinux.txt

3-grep-example-3.png

Notice, the grep command does a good job of specifying the file in which it finds a specific match. This is why the grep command is so useful for searching for strings in files.

3. Filtering or Searching output of another command

grep can, as previously stated, be used to filter or search the output of another command. This is made possible by making use of the command line chaining operator pipe (|).

┌──(traw㉿kali)-[~/articles]
└─$ head -n 12 linuxqoutes.txt | grep "Linux"

4-grep-example-4.png

4. Display Line Numbers Containing Matches

Using the -n option in conjunction with grep, it will display the line numbers containing matches as well as their respective matches:

┌──(traw㉿kali)-[~/articles]
└─$ grep -n "Linux" linuxqoutes.txt

5-grep-example-5.png

5. Making grep search case insensitive

Grep search is case sensitive by default. If you want the search to be case insensitive you can use -i option.

┌──(traw㉿kali)-[~/articles]
└─$ grep -i "LINUX" linuxqoutes.txt

6-grep-example-6.png

In the preceding example, we searched for the word "LINUX" and grep returns the words Linux and LINUX as matching.

6. Using regular expressions with grep

The search string can be a regular expression, which makes grep quite strong. However, I will not go into detail on how to use regexp with grep in this article.

The following example will search for lines which contains any digits from 0 up to 9.

┌──(traw㉿kali)-[~/articles]
└─$ grep "[0-9]" linuxqoutes.txt

7-grep-example-7.png

You can boost the power of your search by employing a regex pattern. There are special grep options that allows you to use a regex pattern.

  • e - enables the use regex patterns.

  • E - enables the use of extended regex patterns.

  • G - enables the use of basic regex patterns.

  • P - enables the use of perl regex patterns.

Here we used extended regular expressions to search for a word which starts with any characters (*) and ends with (sh).

┌──(traw㉿kali)-[~/articles]
└─$ cut -d ":" -f 7 /etc/passwd | grep -E "*sh$"

8-grep-example-8.png

Notice, if we don't use the "-E" option grep won't display anything, this shows that without the "-E" parameter grep command won't recognize the provided pattern.

7. Displaying all the lines that DO NOT match a given pattern.

Another thing you might find useful is to use the "-v" option to reverse the result, eliminating all the lines that match a specific search string:

Here we eliminated lines which do not contains the word "Linus Torvalds":

┌──(traw㉿kali)-[~/articles]
└─$ grep -v  "Linus Torvalds" linuxqoutes.txt

missing option v.png

8. Combining grep options

Grep, like any other Linux command, can combine several options to perform multiple tasks at once. In this case, we combined the -v and -i option to instruct the grep command to be case insensitive with the pattern and to only display lines that DON'T match that given pattern:

┌──(traw㉿kali)-[~/articles]
└─$ grep -vi "Linux" linuxqoutes.txt

1010110-grep-example-10.png

9. Find Exact Match Words.

If you search for the phrase 'Lin,' grep will also return lines containing the words 'Linux' or 'Linus.'

11-grep-example-11.png

Fortunately, grep has the option "-w" which allows it to search and match the exact whole words only. In this case grep didn't find any match because the file "linuxqoutes.txt" doesn't contain the word "Lin".

┌──(traw㉿kali)-[~/articles]
└─$ grep -w "Lin" linuxqoutes.txt

12-grep-example-12png.png

10. Searching in all files recursively

With the grep option "-r", you can execute a recursive search. It will look for the specified pattern in all files in the current directory and its sub-directories. Notice, I tacked "-i" with "-r option" to make the search case insensitive:

┌──(traw㉿kali)-[~/articles]
└─$ grep -ir "linux"

13-grep-example-13.png

11. Print only names of FILEs with matching lines

Grep displays the matching lines by default. If you only want to know which files contain the string, use the following grep options:

  • r - recursively search every file in the current dir

  • l - print only names of FILEs with matches

  • i - this option is optional, I have added it since I want my search to be case insensitive:

┌──(traw㉿kali)-[~/articles]
└─$ grep -irl "linux"

14-grep-example-14.png

12. Print only names of FILEs with no matching lines

If you only want to know which files do contain the string, use the following command:

-r - recursively searches every file in the current directory. -L - print only names of FILEs without the matching lines.

┌──(traw㉿kali)-[~/articles]
└─$ grep -rL "Ubuntu"

15-grep-example-15.png

Bonus

To help you remember to grep commands while using Linux I have made this cheat sheet. So feel free to download and save it for quick reference.

grep cheatsheet.png

Conclusion

Those were some straightforward Grep examples. If you read the man page for this command, you'll notice that it has a plethora of additional parameters and uses. This information should be sufficient to help you understand the Linux grep command and how to use it.

That's all! Thank you for getting this far. I hope you find this article useful. If you did find this article valuable:

  • Toss us a follow for more amazing articles on Linux, sysadmin and security

  • be sure to share with other Linux folks who you think might be useful to them.

and check me out on Twitter, Instagram Mastodon, GitHub and Join our Discord server to learn with us.

1
Subscribe to my newsletter

Read articles from sysxplore directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

sysxplore
sysxplore

Helping people master Linux | @kubesimplify contributor. Daily blogs on Linux🐧, Security🐱‍💻 and Networking🌐.