# All About GREP in Linux!!

Hello Community,

I am grateful to @[Shubham Londhe](@TrainWithShubham) for taking initiative and assisting the DevOps Community via the TrainwithShubham youtube channel with a ton full of knowledge.

So, as a helping hand for Community tried to share my knowledge on Grep Command.

**GREP(Global Regular Expression Print)**: "$grep" command is a filter that searches a file according to a particular pattern of characters and displays all lines that contain the searched pattern. The pattern that is searched in the file is referred to as the regular expression.

**Let's take a look at some use cases.**

**Case 1: Suppose you want to find a file or a directory with a particular word in the entire Linux FileSystem or in a particular path.**

* $ **grep -r DevOps /**: This command recursively finds all the files/directories which has the word "DevOps" mentioned in it throughout **the Linux File System.**
    
    In the above command -r specifies finding recursively.
    

**<mark>Note: Executing all the below commands in the directory named "DevOps-zero-to-hero" in that again "prod" directory in the prod directory on production.txt.</mark>**

* $ **grep -r production /home/ubuntu:** This command recursively finds all the files/directories which has the word "production" mentioned in it but in the only directories that we have written in the command **i.e /home/ubuntu** as shown in below image.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680002574948/36c70313-eb54-4fde-811e-853090b05dde.png align="center")

**Case 2: Suppose you want to display the Line number of the filtered file.**

* $ **grep -n "INFO" production.txt:** This command **show's line number while displaying the filtered output using grep -n** as shown in the below image. The very first column in the below image indicates the line number of the word INFO in the file.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680003234268/99e460b6-038a-4ec7-a20c-59f2f8772b4a.png align="center")

**Case 3: Suppose you want to find the lines from a file that are Matched and Not matched with the specified pattern.**

$ **grep EVENT production.txt:** This command helps you display the lines that are matching with the specified search string.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680010572230/12be7590-5532-40df-a641-b1e7a19d86f4.png align="center")

$ **grep -v INFO production.txt**: This command helps you **display the lines that are not matched with the specified search string pattern using the -v option.** 

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680004450711/a7bb485f-d7c2-46f2-9545-0b031e387d6a.png align="center")

**Case 4: Suppose you want to find the lines from the file starting/ending with the particular specified pattern.**

**$ grep "^03/22 08:52:51" production.txt:** This command **list down the lines starting with "03/22 08:52:51".The ^ regular expression pattern specifies the start of a line**. This can be used in grep to match the lines which start with the given string or pattern. 

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680010170852/e88c4839-7f5b-4ada-b699-89cb86b2f002.png align="center")

$ **grep "=0$" production.txt**: This command **list down the lines ending with "=0".The $ regular expression pattern specifies the end of a line.** This can be used in grep to match the lines which end with the given string or pattern. 

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680005065306/6ce6c455-ccae-4334-b925-9145dd0c29a4.png align="center")

**These are some of the Use Cases in which the GREP command helps us to filter things in many different ways making it simpler to check. I hope this blog has helped you with good information about the grep command. Thanks for investing time & reading this blog Like & Share this blog with your friends. Do Comment & let me know your views.**
