12 Practical Grep Command Examples In Linux (2024)

In 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 it is the Grep command on 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 :

12 Practical Grep Command Examples In Linux (1)

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.

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

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

12 Practical Grep Command Examples In Linux (2)

2. Finding all occurrences of a string 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

12 Practical Grep Command Examples In Linux (3)

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"

12 Practical Grep Command Examples In Linux (4)

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

12 Practical Grep Command Examples In Linux (5)

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

12 Practical Grep Command Examples In Linux (6)

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

12 Practical Grep Command Examples In Linux (7)

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

  • e - enables the use of 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$"

12 Practical Grep Command Examples In Linux (8)

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 contain the word "Linus Torvalds":

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 options 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

12 Practical Grep Command Examples In Linux (9)

9. Find Exact Match Words.

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

12 Practical Grep Command Examples In Linux (10)

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

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

12 Practical Grep Command Examples In Linux (11)

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"

12 Practical Grep Command Examples In Linux (12)

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"

12 Practical Grep Command Examples In Linux (13)

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 search every file in the current directory.-L - print only names of FILEs without the matching lines.

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

12 Practical Grep Command Examples In Linux (14)

Bonus

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

12 Practical Grep Command Examples In Linux (15)

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 found this article valuable:

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

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

Like the blog? Follow Kubesimplify on Hashnode, Twitter and LinkedIn. Join our Discord server to learn with us.

12 Practical Grep Command Examples In Linux (2024)
Top Articles
What Do Guys Like to be Called? 20 of Their Favorite Nicknames
Star Dragons
Amy Davis No Wedding Ring
Bhizzi Lpsg
Apolonia's Prime Steakhouse Okeechobee Fl
Interview with Valeria Golino, member of the Feature Films Jury - Festival de Cannes
Trauma Care | FMOLHS
Dfw To Anywhere Google Flights
Todoroki Oc
Current Inmate Database & Daily Arrests
Bmp 202 Blue Round Pill
Dragon Block C Resource Pack
Hca Gulf Coast Scheduler
Just Busted Lewisburg Tennessee
1Tamilmv Unblock
San Diego Terminal 2 Parking Promo Code
What Number Is 0.1 Less Than 11.6
Fed meeting recap: Chair Jerome Powell defends central bank's decision to go big with first cut
Play Billiards Near Me
Van der Poel kleurt EK op kasseien, maar Merlier wint sprint vlak voor Kooij
Ky Fl Basketball Game Today
Profile By Sanford Recipes
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Directions To 295 North
Buhsd Studentvue
Final Schedule Cmu
Wenig Kooperation mit AfD auf kommunaler Ebene in Ostdeutschland
Couponsky Net
Parent Portal Pat Med
Studentvue Stockton Ca
Craigslist Alo
First Daughter | Rotten Tomatoes
Centricity Time And Attendance Premier Health
1800 Water Damage Princess Anne Va
Ezpz Escape Answer Key
Assad continues to prove reliability despite Cubs' loss
Joshua's Law Unit 7 Lesson 2
Understanding "X marks the spot" Idiom: Meaning, Origins & Usage - CrossIdiomas.com
Itslunarliv Leaked Video
Kamzz Llc
Nikolitsa Gloria Stephanopoulos
Aultman.mysecurebill
Pmrank 2022
Tropical Smoothie Delivery Near Me
70 Fantastic creatures from mythology
Remote Desktop Web Access | Department of Computer Science
Sams La Habra Gas Price
Game Akin To Bingo Nyt
Madelyn Cline Surgeon
WSOP NEWS: THE-WORLD-SERIES-OF-POKER-RELEASES-INITIAL-DATES-FOR-2024-25-WSOP-CIRCUIT-SCHEDULE
Savage Funniest Texts Drunk
Onelook Com Thesaurus
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6509

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.