grep is a tool for filtering text in Linux systems. We can get a specific text or look for a pattern. grep is a tool used daily operation by Linux administrators. We will look at simple usage types in this tutorial. Grep can be used to find a word inside a folder. Grep name came from /g/r/ep expression.

Install Grep in Linux

We hope and expect that most of the modern Linux distributions have installed grep by default. Here is if not

Ubuntu, Debian:

We will use the apt-get command in order to install grep tool.

$ sudo apt-get install grep -y

CentOS, Ubuntu:

We will use dnf or yum in order to install grep package.

$ sudo dnf install grep -y

OR

$ sudo yum install grep -y

Simple Grep Some Text From a Text File

We start with very simple usage of grep by just getting some text from a file. This is our text file named test.txt

httpd-2.4.23.tar.bz2 httpd-2.4.23.tar.bz2.asc mytext.txt newtextfile nmap-7.31.tgz output2 percona-release_0.1-4.xenial_all.deb PNGGRAD8RGB2.PNG PNGGRAD8RGB3.PNG PNGGRAD8RGB4.PNG pnggrad8rgb.jpg pnggrad8rgb.png source.zip ssh_script test.zip thumb.png thumb.pnggrad8rgb.png thumb.thumb.png thumb.thumb.pnggrad8rgb.png thumb.thumb.thumb.png yourcommand

Search for PNG in the file named mytext.txt . We will first provide the PNG the name we want to search and then the file or input where we will search.

$ grep PNG mytext.txt

Simple grep example

grep searches whole file and extracts lines that provide text “PNG”. The matched part will be also highlighted with red or similar colors.

Search Strings Case-Insensitive

By default grep searches case sensitive which means upper and lower case chars are interpreted as different. If we are looking for a hostname in a file it is not important uppercase and lowercase so we will turn off case sensitivity for grep. We will provide -i option to for case insensitive search.

$ grep -i Thumb mytext.txt

Search Strings Case-Insensitive

As we see that turning off case sensitivity produces more results for the same text.

Search Recursive with Grep

We have searched only in one file for now. If there are hundreds of files with multilevel hierarchy searching one by one is not feasible. So searching recursively is the best solution for this situation. We will provide the -r option which is the shortcut form of the --recursive .

$ grep -r This

Search Recursive with Grep

This will search from starting the current path to downward recursively. If we provide a path it will start from there like below. In the following example, we will search This term in the whole Linux root file system.

$ grep -r This /

Search for a Word with Grep

We have searched up to now for a text. But we may search for a word which is separated by spaces. This will search only the whole word match which is test in this case. We will use the -w option and provide the word we want to search like below.

$ grep -w test mytext.txt

Search For Line Start

We can look for a line start and line and with the following command. We will use ^ as the line start sign and provide the term we want to match. In the following example, we ware looking lines starting with t letter.

$ egrep ^t mytext.txt

Search For Line End

We are looking for lines that end with g character. We are expecting the line end control character after g . If there are spaces it will not work. We will use $ as the end of the line sign and put the characters before this sign.

$ egrep g$ mytext.txt

Reverse Match in Grep

We have searched for existing terms up to now but can search for non-existing terms. We can name this negative match or reverse match. We will use the -v option and provide the term we do not want to match. In the following example, we will list lines that do not contain the p letter.

$ grep -v p mytext.txt

Search Non-Existing Text Lines in Grep

We are looking for lines that do not have p character. This is called also reverse regex because we list negative of search.

Look for Line Number in Grep

If we are looking at some texts upper and lower lines we can provide the count of lines to be shown. It will show the surrounding lines too. We will use -nC option with the number of neighbor lines we want to list or show. In the following example, we want to show a single line around the match and provide 1 like -nC1 as an option.

$ grep -nC1 test.zip mytext.txt

Look for Line Number in Grep

Show Only File Names

Normally grep will show filenames and the lines that match. We can list only filenames by using -l option. In the following example, we will list the files that contain the term This .

$ grep -r 'This' -l

Show Only File Names

This will list files that have This string. We are searching recursively and listing the names of the files.

Match For Given File Extensions

We want to grep in just C header files how can we do this? We must supply the –include parameter with extension.

$ grep -r --include \*.h 'stdio' -l * /lib

Grep For Certain File Extensions

Only Print Match of Grep

We may want to get only the match string. Say we need IP addresses and only need the IP address nothing else. -o option only prints exactly matching strings

$ grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /var/log/auth.log

Only Print Match of Grep

egrep Command

Egrep command is grep -E version which will extend the functionality of grep and provide regular expression support. Grep has 3 different pattern types. First one is Basic which is known BRE second is Extended known as ERE and the third one is Perl PRCE . Below we will look at some examples of grep regular expression support. Here are some Regular Expression Operators. This is our example text. We named this file as myinput

pof tut com poftut.com poftut

? makes preceding item optional and matched at most once.

$ egrep "of?" myinput

makes preceding item will be matched zero or more time

$ egrep "o*" myinput

makes preceding item will be matched one or more time

$ egrep "o+" myinput

{n} makes preceding item will be matched exactly at n time

$ egrep "m{1}" myinput

{n,} makes preceding item will be matched at least n time

$ egrep "(com){1}" myinput

{,m} makes preceding item will be matched at most m time

$ egrep "(com){,1}" myinput

{n,m} makes preceding item will be matched at least n at most m time

$ egrep "(com){1,1}" myinput

Grep And Operator

There is no grep And operator specifically in grep we will simulate it by piping multiple greps together like below.

$ grep "om" myinput | grep "c"

And logic operation can be defined with.

Grep Or Operator

Or is a logic operation to find one of the provided items. We will look com or o strings in our file. Or is defined with | .

$ grep "com\|o" myinput

Grep Or Operator

If we want to use it with egrep with regular expression support pipe | can be used as a regular expression grep operator like below.

$ egrep "(com){1,1}|o" myinput

Grep Or Operator

Colorizing Grep for Colorful Output

Our grep may find a lot of results. This is very hard to examine. Here colorizing will make our work easier. To make grep results colorful use –color

$ grep --color t mytext.txt

Exclude Unwanted String

We can remove an unwanted string from our search by piping two greps like below. First, grep removes the unwanted string. Second, grep is for our normal search.

$ grep -v 'png' mytext.txt | grep 't'

Count String Occurrences

We may count the string occurrence. This will give the total number of the string that matches.

$ grep -c 'png' mytext.txt