# Grep command in linux

Grep(global regular expression print) command is used in Linux to find and search using regular expressions throughout the Linux system.

`syntax: grep 'string' filename`

or

`syntax: filename grep 'string'`

`eg -> grep -i 'warning' logfile.txt`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682677931561/9e93e242-fc5f-41b1-a7fe-9fa39c87eb70.png align="center")

To search recursively in all the directories -r option

`grep 'config' -r /etc/nginx`

In the above command, we're finding a config file inside the /etc/nginx directory. By using -r, it will find all the sub-directories inside the nginx directory

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682678423535/c6b239f5-dcda-4624-8f19-027576717748.png align="center")

To count the lines where strings are matched with -c option

`grep -c 'INFO' logfile.txt`

This command will display the number of lines in which the INFO text is present in the logfile.txt

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682678660306/1b560695-3e03-480a-9667-42358be0843a.png align="center")

To display the line number where the search pattern contains with -n option

`grep -n 'WARNING' logfile.txt`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682678818175/d51fda34-d70d-4e69-b8fc-90912a0f9e7b.png align="center")

To ignore case sensitivity with the -i option

`grep -i 'info' logfile.txt`

To invert the output of a file with the -v flag

`grep -vi 'info' logfile.txt`

This will print everything on the console that doesn't contain info

**Redirecting the results from other commands to grep by using pipe |**

`dpkg -l | grep 'package-name'`

eg -

`dpkg -l | grep "chrome"`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682679491336/1160af5d-a702-4d1d-9c84-0b374f8ce282.png align="center")

here dpkg command will list the install packages inside the Linux system and with the help of pipe we check that the Chrome browser is installed in the system

**grep with REGEX**

A REGEX or REgular EXpression is a sequence of characters that is used to match pattern.

eg -&gt;

```plaintext
^      Matches characters at the beginning of a line
$      Matches characters at the end of a line
"."    Matches any character
[a-z]  Matches any characters between A and Z
[^ ..] Matches anything apart from what is contained in the brackets
```

**To print certain characters from a file**

`syntax - grep ^character filename`

To display the lines begin with the number "0" in our logfile.txt file

`grep ^0 logfile.txt`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682683680815/5f5e9e2c-d05a-41fe-9ba5-a17ca1390839.png align="center")

To display the lines end with character "p"

`grep p$ logfile.txt`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682683775089/0c434c42-123f-411a-9512-1862af568494.png align="center")

**Colorizing grep results with --color flag**

If you're working on a system that doesn't display the search pattern in a different color then use `--color` flag.

`grep --color 'INFO' logfile.txt`

**For more grep options**

If you need to learn more about grep command usage, then use `--help` will display the usage of all flags and options that can be used with the grep command.

`grep --help`

## Thank you
