Grep command in Linux and Unix with examples

Published on August 26, 2023

The grep command in Unix and Linux stands for "global regular expression print". It searches for a file or files using a pattern of characters and prints the matching characters to the standard output.

Grep command in Linux / Unix

Grep command

The syntax for grep is:

grep [OPTIONS] PATTERNS [FILE]

You can find more about grep with this command:

$ grep --help

You will see several options that grep can use. We will work on practical examples using those options. If you would like anything in specific, please contact us with your query.

Basic Grep command

For our testing, we will change to the web server log directory. In our case, we will chdir to the Nginx log directory and run our grep commands there.

I want to search Nginx's access.log for all entries made by Yandex. Yandex leaves a signature YandexBot or contains the URL yandex.com. Let me just grep on the string Yandex.

$ grep 'Yandex' access.log
789.789.789.1 - - [25/Aug/2023:00:19:02 -0400] "GET /blog/python-raspberrypi/ HTTP/1.1" 200 7023 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) X-Middleton/1"
789.789.789.2 - - [25/Aug/2023:00:19:02 -0400] "GET /blog/install-python/ HTTP/1.1" 200 7182 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) X-Middleton/1"
789.789.789.4 - - [25/Aug/2023:00:19:02 -0400] "GET /blog/ HTTP/1.1" 200 6735 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) X-Middleton/1"
789.789.789.4 - - [25/Aug/2023:00:19:03 -0400] "GET /blog/install-python-debian/ HTTP/1.1" 200 7938 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) X-Middleton/1"
789.789.789.5 - - [25/Aug/2023:00:19:48 -0400] "GET /blog/philippines-packing-list/ HTTP/1.1" 200 12540 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) X-Middleton/1"

Please note that I have masked Yandex's IP addresses with 789.789.789.1-10, to hide the real IP addresses. Read why I mask IP addresses in examples.

Create text file Gitanjali35.txt

We will create an ASCII text file named Gitanjali35.txt. Copy this content into it:

Where the mind is without fear and the head is held high;
Where knowledge is free;
Where the world has not been broken up into fragments by narrow domestic walls;
Where words come out from the depth of truth;
Where tireless striving stretches its arms towards perfection;
Where the clear stream of reason has not lost its way into the dreary desert sand of dead habit;
Where the mind is led forward by thee into ever-widening thought and action
Into that heaven of freedom, my Father, let my country awake.

This is the 35th poem of Gitanjali written by Rabindranath Tagore in 1913. The original was written in Bengali in 1912.

You can also directly download the file here

We will work out all our grep command examples using this text file.

Grep command with examples

Search for lines beginning with Into

Use the regular expression pattern ^ as a prefix to display lines matching the start of the lines.

grep '^Into' Gitanjali35.txt

Output:

$ grep '^Into' Gitanjali35.txt
Into that heaven of freedom, my Father, let my country awake.

Search for lines ending with a semicolon ;

Use the regular expression pattern $ as a suffix to display lines matching the end of the lines.

grep ';$' Gitanjali35.txt

Output:

$ grep ';$' Gitanjali35.txt
Where the mind is without fear and the head is held high;
Where knowledge is free;
Where the world has not been broken up into fragments by narrow domestic walls;
Where words come out from the depth of truth;
Where tireless striving stretches its arms towards perfection;
Where the clear stream of reason has not lost its way into the dreary desert sand of dead habit;

Search for lines containing any of the vowels p or F

Use the regex pattern [pF] to match any of the options with the lines.

$ grep '[pF]' Gitanjali35.txt

Output:

Where the world has not been broken up into fragments by narrow domestic walls;
Where words come out from the depth of truth;
Where tireless striving stretches its arms towards perfection;
Into that heaven of freedom, my Father, let my country awake.

Search for lines containing any of the letters in the range x-z

Use the regex pattern [x-z] to match any letter in the range x-z with the lines.

$ grep '[x-z]' Gitanjali35.txt

Output:

$ grep '[x-z]' Gitanjali35.txt
Where the world has not been broken up into fragments by narrow domestic walls;
Where the clear stream of reason has not lost its way into the dreary desert sand of dead habit;
Where the mind is led forward by thee into ever-widening thought and action
Into that heaven of freedom, my Father, let my country awake.

Search for lines and line numbers matching the characters of

Use the option -n to find the line number as well.

$ grep -n 'of' Gitanjali35.txt

Output:

$ grep -n 'of' Gitanjali35.txt
4:Where words come out from the depth of truth;
6:Where the clear stream of reason has not lost its way into the dreary desert sand of dead habit;
8:Into that heaven of freedom, my Father, let my country awake.

Search for lines containing the string Into or into. Search for into case-insensitive

Use the option -i to search for a string case-insensitive.

$ grep -i 'into' Gitanjali35.txt

Output:

$ grep -i 'into' Gitanjali35.txt
Where the world has not been broken up into fragments by narrow domestic walls;
Where the clear stream of reason has not lost its way into the dreary desert sand of dead habit;
Where the mind is led forward by thee into ever-widening thought and action
Into that heaven of freedom, my Father, let my country awake.

If you do not use -i:

You can also search for Into or into like this:

grep '[Ii]nto' Gitanjali35.txt

Output:

$ grep '[Ii]nto' Gitanjali35.txt 
Where the world has not been broken up into fragments by narrow domestic walls;
Where the clear stream of reason has not lost its way into the dreary desert sand of dead habit;
Where the mind is led forward by thee into ever-widening thought and action
Into that heaven of freedom, my Father, let my country awake.

Search for into

$ grep 'into' Gitanjali35.txt

Output:

Where the world has not been broken up into fragments by narrow domestic walls;
Where the clear stream of reason has not lost its way into the dreary desert sand of dead habit;
Where the mind is led forward by thee into ever-widening thought and action

Search for Into

$ grep 'Into' Gitanjali35.txt

Output:

Into that heaven of freedom, my Father, let my country awake.

Search for a pattern Yandex or yandex while tailing a growing log file access.log

You can pipe the tail output into a grep command like this:

$ tail -f access.log | grep -i 'yandex'

Count the number of lines containing the string bingbot in the log file access.log

Use the -c option.

$ grep -c 'bingbot' access.log

Search for Chrome/ followed by 0 or more digits in the log file access.log and print only the top 5 results

Use the * modifier and pipe it to head -5

grep 'Chrome/[0-9]*' access.log | head -5

Output:

$ grep 'Chrome/[0-9]*' access.log | head -5
789.789.789.1 - - [25/Aug/2023:00:10:01 -0400] "GET /lyrics/pa/National_Anthem/ HTTP/2.0" 200 1793 "https://www.google.com/" "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36"
789.789.789.1 - - [25/Aug/2023:00:15:01 -0400] "GET /lyrics/ta/Walang_Imposible_sa_Diyos/ HTTP/1.1" 200 1671 "https://ph.search.yahoo.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
789.789.789.1 - - [25/Aug/2023:00:16:44 -0400] "GET /lyrics/op/Dadalhin/ HTTP/2.0" 200 1905 "https://www.google.com/" "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36"
789.789.789.1 - - [25/Aug/2023:00:26:40 -0400] "GET /lyrics/ta/Salamat_Sa_Iyo/ HTTP/2.0" 200 1766 "https://www.google.com/" "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36"
789.789.789.1 - - [25/Aug/2023:00:26:41 -0400] "GET /lyrics/ta/Salamat_Sa_Iyo/ HTTP/2.0" 200 1766 "https://www.google.com/" "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36"

Any Grep requests?

If you are looking for a more complex grep command than what is here, let me know. I will append your question and my solution here.


Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on August 26, 2023