Learn $ grep command with 15 examples
In this blog post we are gonna talk about one of the most basic command of linux which is grep, also we are going to see the 15 different examples on "How to use grep?".
It doesn't matter you are an experienced developer or you are just a beginner out of these 15 examples you're just gonna learn something new.
What is grep?
grep command in the linux is used to search a text present inside a file.
Here is an example of a grep command -
1#grep command syntax
2
3grep <text_to_search> <file_name>
The command begin with the keyword grep followed by -
- <text_to_search> : The text string which you want to search.
- <file_name> : The file name in which you want to search.
Table of Content
- How to use grep command in Linux
- How to grep for same string in multiple files at the same time?
- How do I recursively grep all directories and subdirectories?
- How do I use grep to search the current directory for all files
- grep invert search with context
- Match exact string using grep
- grep Search Case Insensitive String
- grep and regular expression
- How To Count All Matches of a String With grep
- How can I format my grep output to show line numbers
- How do I limit the number of results returned from grep?
- How To Grep From Files and Display the File Name
- grep command with regular expression to search string starts with
- Linux find and grep command together
- Linux Shell commands cat and grep
1. How to use grep command in Linux?
To start with our very basic command of a grep to search for a text and the text can be anything.
Example-
- Text I want to search - jhooq
- In which file i want to search - text-file-1.txt
How you need to write a command?
1#Search text inside a file
2
3grep jhooq text-file-1.txt
Here the file name is text_file1.txt, so this is the file inside i'll be searching the keyword jhooq.
Let's have a very quick look at content of text_file1.txt file -
1# text_file1.txt file content with line number
2
31. 79 video
42.
53. jhooq - Fargate and ECS(Elastic container service).
64.
75. How to use fargate with docker and ECS
86.
97. jhooq - grep tutorial and its usage
108. jhooq - linux command line utilites
119.
1210. Jhooq
Let's run our first grep command grep jhooq text-file-1.txt -
Here is the output -
*Note- The grep command is case sensitive so it will not search for uppercase text
2. How to grep string in multiple files at the same time?
In the previous example we have seen like how to search a particular text inside a single file.
But what about if you want to search the similar text that is jhooq into multiple file.
You can do that and the syntax is much similar to the previous command -
Example-
- Text I want to search - jhooq
- In which file i want to search -
- text-file-1.txt
- text-file-2.txt
How you need to write a command?
1#Search text inside multiple files
2
3grep jhooq text-file-1.txt text-file-2.txt
This command will just search for the keyword jhooq inside two files that is text_file1 as well as text_file2.
Here is the output -

Let's have a very quick look at content of text_file1.txt and text_file2.txt file -
1# text_file1.txt
2
31. 79 video
42.
53. jhooq - Fargate and ECS(Elastic container service).
64.
75. How to use fargate with docker and ECS
86.
97. jhooq - grep tutorial and its usage
108. jhooq - linux command line utilites
119.
1210. Jhooq
1# text_file2.txt
2
31. 80 Video
4
52. jhooq - Docker and AWS ECR(Elastic contianer registry)
6
73. Docker how to push docker image to ECR%
3. How do I recursively grep all directories and subdirectories?
The next example of a search command is to search for a text inside the sub-directory.
Example-
- Text I want to search - jhooq
- Files present in current directory as well as sub-directory -
1# directory structure
2# current working directory - text-file-1.txt, text-file-2.txt
3# sub directory - text-file-3.txt
4
5 |-text-file-1.txt
6 |
7 |-text-file-2.txt
8 |
9 |-sub-directory
10 |
11 |-text-file-3.txt
How you need to write a command?
1#Search text inside directories and subdirectories
2
3grep -r jhooq *
Here is the output -

4. How do I use grep to search the current directory for all files?
Moving to the next command This command actually search into the current working directory but not into the sub directories because we don't have that recursive flag over here.
Example-
- Text I want to search - jhooq
- Files present in current directory -
1# directory structure
2# current working directory - text-file-1.txt, text-file-2.txt
3# sub directory - text-file-3.txt
4
5 |-text-file-1.txt
6 |
7 |-text-file-2.txt
8 |
How you need to write a command?
1#Search text inside directories
2
3grep jhooq *
Here is the output -

5. grep invert search with context
The next example command is for inverse search so what does it mean by the inverse search - it will exclude the keyword jhooq and the line where it found.
Example-
- Text I want to search - jhooq
- Files present in current directory as well as sub-directory -
1# directory structure
2# current working directory - text-file-1.txt, text-file-2.txt
3
4 |-text-file-1.txt
5 |
6 |-text-file-2.txt
7 |
How you need to write a command?
1#Search text inside directories
2
3grep -v jhooq *
Here is the output -

6. Match exact string using grep
Till now we have seen some examples and now we are familiar with using the grep command. So next thing which we are going to try is to search the exact word.
What does it mean by exact word?
First of all let's try some other command so i'm just going to use this command - grep hooq *
In the above output in which it is searching for the text hooq but it is a part of a text jhooq. So that is why it is returning back with those result.
But what if you wanna search the exact word hooq and it should not be a part of another word.
Example-
- The exact text I want to search - hooq
- Files present in current directory -
1# directory structure
2# current working directory - text-file-1.txt, text-file-2.txt
3
4 |-text-file-1.txt
5 |
6 |-text-file-2.txt
How you need to write a command?
1#Search text inside directories
2
3grep -w hooq *
Here is the output -
As you can there is nothing returned because exact hooq keyword does not exist

7. grep Search Case Insensitive String
The next example which i would like to ignore the upper case and the lower case. I mean how to handle the case sensitivity in the grep command ?
By default grep command is case sensitive. Let's take an example over here -
1# This command is case sensitive and it will search for text Jhooq where letter J is uppercase
2
3grep Jhooq text_file1.txt
After running the above command you will see only one result because in text-file-1.txt there is only one occurence -

How you can ignore this case sensitivity ?
You need to use the flag dash -i over here and that will exclude the case sensitivity and you can search all the keywords in your text
file.
1grep -i Jhooq text_file1.txt
After the running the above we can see all the results irrespective of the uppercase or lowercase.
Here is the output -

8. grep and regular expression
Next example which include is the regular expression, so you might have been working with the other programming languages and you might have an idea how the regular expression works.
grep supports the regular expression, i'm just going to take a very basic example and how you can write your regular expressions over here -
1#grep command with regular expression to search text which start with letter jh
2
3grep ^jh *
The above command it is just going to evaluate that regular expression and search for those keywords.
It is only going to search for the words which start with the lower case j and h so here you can see these are the three occurrences in text_file1 and one occurrence in text_file2.
Here is the output -
9. How To Count All Matches of a String grep command
Moving to the next example so here we will be counting the match or number of occurrence of a particular keyword inside the files -
- text_file1.txt
- text_file2.txt
1# grep Count All Matches for string docker
2
3grep -c Docker *
Here is the output - In the text_file1.txt the count is 0 for the text Docker While In the text_file2.txt the count is 2 for the text Docker

For reference here is text_file2.txt
1# text_file2.txt
2
31. 80 Video
4
52. jhooq - Docker and AWS ECR(Elastic contianer registry)
6
73. Docker how to push docker image to ECR%
10. How can I format my grep output to show line numbers
Next command we are gonna try for if you want to print the line number where the particular word is occurred.
I'm just running the same command as of previous one but this time i'm passing the flag -n and I'm just trying to find the line number of that particular text .i.e. docker that start with the lowercase d.
1# grep format my grep output to show line numbers
2
3grep -n Docker *
Here is the output -
11. How do I limit the number of results returned from grep?
In this example we are gonna see how to limit the number of results returned from grep so let's take an example once again grep -m1 Docker *.
1# grep limit the number of results returned
2
3grep -m1 Docker *
There is a flag for that and that flag is -m1- where m is the flag and 1 is the line number which you want to show.
So if you want to show only one line then just type 1 over here and if you are interested in more line then just type the number of lines which you want to show on to your terminal as an output.
Here is the output -
In the blow screenshot you can see there is only one output row -
12. How To Grep From Files and Display the File Name?
Taking the grep command further what if you only want to print the file name where the particular keyword has occurred.
Here we are taking the example of a text_file1, text_file2 which is present into the sub directory so i just wanted to print the name of the file not the entire string or entire line.
To achieve this what you can do you just need to supply this flag -l and it will list out the file name where that particular keyword is present.
1# grep print the file name
2
3grep -l jhooq *
Here is the output -
In the blow screenshot you can see file name -
13 . grep command with regular expression to search string ending with
grep supports the regular expression, i'm just going to take a very basic example and how you can write your regular expressions over here to search string which ends with letter utilites
1# grep search text which ends with letter `utilites`
2
3grep "\utilites$" *
Here is the output -
14. Linux find and grep command together
This is a bit advanced command because in this command we will do two tasks -
- Find files using
findcommand - Search the text using
grepcommand
1# find and grep command together. The final text we will be searching is jhooq
2
3find . -name '*text-file-1.txt*' -print0 | xargs -0 grep -H "jhooq"
Here is the output - First we used the find command to search files and then piped(joined) the output to perform search.

15. Linux commands cat and grep
Just like previous command here also we are going to join two command cat and grep.
1# find and grep command together. The final text we will be searching is jhooq
2
3cat text-file-1.txt | grep jhooq
Here is the output -

Conlcusion
I hope the above examples would help you to get more firm grasp onto the grep command and also you could use in
your day to day devops operations.