Best examples of Linux grep commands

This article shows you how to use the Linux grep command on Linux. GREP stands for Global Regular eExpression Print. The Linux grep command is used to search in Linux and Unix. Here we learn some very important and useful examples of Linux grep commands.

The best examples for Linux grep commands

Syntax for the grep command:

grep [Options] [String for Search] [File Path]

Use the Linux grep command to find a string

I have a file called file.txt with some text as shown in the issue below.

[[email protected] ~]# cat file.txt 
Linux is a Open Source Operating System.

LINUX IS A OPEN SOURCE OPERATING SYSTEM.

linux is a open source operating system.

Let’s look for a string, i.e. H. Linux from file file.txt,

[[email protected] ~]# grep linux file.txt  # Search for a String
linux is a open source operating system.

Color the string you are looking for

Find a string by highlighting it in color using the grep command – -Colour,

[[email protected] ~]# grep --color linux file.txt   # Search for string by Highlighting it with color
linux is a open source operating system.

Let’s take an example. Here I look for users itsmarttricks in the / etc / passwd File with grep command.

[[email protected] ~]# cat /etc/passwd | grep --color itsmarttricks   # Searching for a User using grep Command
itsmarttricks:x:501:501::/home/itsmarttricks:/bin/bash

You can also search for an installed package. Here I am looking for whether the vsftpd package is installed or not.

[[email protected] ~]# rpm -qa | grep vsftpd   # Searching for a Installed Package
vsftpd-2.2.2-24.el6.x86_64

Use the grep command to find strings that are not case sensitive

We can use the grep with argument command to find strings that are case insensitive -I, Here I am looking for String Linux and you’ll find in the output below that I can search all strings, regardless of whether they contain upper or lower case letters.

[[email protected] ~]# grep --color -i linux file.txt   # Searching for Case Insensitive Strings
Linux is a Open Source Operating System.
LINUX IS A OPEN SOURCE OPERATING SYSTEM.
linux is a open source operating system.

Find a string in multiple files with the grep command

You can search for a string from multiple files. Here I have a directory called Data which contains two files, i.e. a.txt and b.txt, and i will search for string Linux from both files. Refer to the sample output below.

You can see in the output below that we can search for the string Linux Both in the files and in the output, the search string is displayed with the file name from which it was searched.

[[email protected] data]# pwd
/root/data
[[email protected] data]# ls
a.txt  b.txt
[[email protected] data]# grep --color -i linux a.txt b.txt  # Searching from Multiple Files
a.txt:linux is a open source operating system.
b.txt:Linux is a Open Source Operating System.

Search for multiple strings

You can search for multiple strings. Here I am looking for strings, i.e. H. Linux. itsmarttricks. to open of file.txt,

[[email protected] ~]# grep -i 'linux|itsmarttricks|Open' file.txt   # Search for Multiple Strings
Linux is a Open Source Operating System.
LINUX IS A OPEN SOURCE OPERATING SYSTEM.
linux is a open source operating system.
welcome to itsmarttricks.com
i love itsmarttricks.com

Search for a string in multiple wildcard files

We can use a wildcard to search with the grep command. For example, I’m looking here for string Linux from all available text files. See the following command.

Syntax: grep -i [string] [path of .txt files]

[[email protected] data]# ls
a.txt  b.txt  c.txt
[[email protected] data]# grep -i linux *.txt   # Searching from all .txt files
a.txt:linux is a open source operating system.
b.txt:Linux is a Open Source Operating System.
c.txt:LINUX IS A OPEN SOURCE OPERATING SYSTEM.

Use the grep command to search for “reverse match”

Another nice feature that we have in grep is the inverted search. Invert search means that only the rows are searched in which the search string is not contained. For example, I’m looking for the string here itsmarttricks, In this case, grep searches only the rows that are not included itsmarttricks String.

[[email protected] ~]# cat file.txt 
Linux is a Open Source Operating System.

LINUX IS A OPEN SOURCE OPERATING SYSTEM.

linux is a open source operating system.

welcome to itsmarttricks.com

i love itsmarttricks.com
[[email protected] ~]# grep -v itsmarttricks file.txt  # Search for Invert Match
Linux is a Open Source Operating System.

LINUX IS A OPEN SOURCE OPERATING SYSTEM.

linux is a open source operating system.

Find the matching regex number with the grep command

We can use grep to search for the command by using regex. For example, I’m using regex here 0-9, means that grep searches all lines that contain a number of 0 to 9, See the following command.

[[email protected] ~]# grep '[0-9]' test.txt  # Searching using Regex
1
2
3
4
5
20 grep command with examples
6
7
8
9
0

As we can also put regex for letters as the number regex a-z, See the following command.

[[email protected] ~]# grep '[a-z]' file.txt  # Searching using Regex
Linux is a Open Source Operating System.
linux is a open source operating system.

[[email protected] ~]# grep -i '[a-z]' file.txt  # Searching using Regex Case Insensitively
Linux is a Open Source Operating System.
LINUX IS A OPEN SOURCE OPERATING SYSTEM.
linux is a open source operating system.

Search recursively with the grep command

How to search recursively for a string in a directory and also in all subdirectories contained in this directory. Here I have a directory named Data which contains some TXT Files with some content and a subdirectory called testing (i.e. / Data / testing), which also contains some TXT Files.

So I’m going to look for the string here Linux in the register Data and also in all subdirectories contained therein with the command grep with argument -r, See the following command.

[[email protected] ~]# cd data/
[[email protected] data]# pwd
/root/data
[[email protected] data]# ls
test1.html  test1.txt  test2.html  test2.txt  test3.html  test3.txt  testing
[[email protected] data]# cd testing/
[[email protected] testing]# pwd
/root/data/testing
[[email protected] testing]# ls
test1.html  test1.txt  test2.html  test2.txt  test3.html  test3.txt

[[email protected] ~]# grep -r "linux" data/  # Searching Recursively
data/test3.html:welcome to itsmarttricks.com
data/testing/test3.html:welcome to itsmarttricks.com
data/testing/test2.html:welcome to itsmarttricks.com
data/testing/test1.html:welcome to itsmarttricks.com
data/test2.html:welcome to itsmarttricks.com
data/test1.html:welcome to itsmarttricks.com

Enter the line number of the search string with the grep command

grep command with argument -n shows you the line number in which the string is available in the file. Here I look for the User itsmarttricks in the / etc / passwd File.

As you can see in the sample edition below, the online number is available 31 in the / etc / passwd File.

[[email protected] ~]# grep -n itsmarttricks/etc/passwd  # Searching the Line Number Where the String is Available in the File
31:itsmarttricks:501:501::/home/itsmarttricks:/bin/bash

Returns the number of hits that were found for the search string

grep command with argument -c shows the number of times the string is repeated in the file.

[[email protected] ~]# grep -i -c open file.txt  # Searching for the count of Numeber of times the string is repeated in the File
3
[[email protected] ~]# cat file.txt 
Linux is a Open Source Operating System.

LINUX IS A OPEN SOURCE OPERATING SYSTEM.

linux is a open source operating system.

welcome to itsmarttricks.com

Look for exactly matching strings

You can use the grep command with argument to find a string that exactly matches -w, Now you might think that it is an exactly fitting string. Suppose you are looking for the string Linux, then grep looks for the whole word that contains Linux, like itsmarttricks. linux books. Linux training and so on. The exact match, here means that only sought when the word “” is Linux and no other.

[[email protected] ~]# grep -w "welcome" file.txt  # Searching for Exact matched String
welcome to itsmarttricks.com

Look for an exact line

As with exactly matching strings, you can use the grep command with argument to find exactly matching lines -x, See the following command.

[[email protected] ~]# grep -x "linux is a open source operating system." file.txt   # Searching for Exact matched Line
linux is a open source operating system.
[[email protected] ~]# grep -x "linux is a open source operating" file.txt 

Search Anchor Match

You can search for matching anchors at the beginning and end of the line. For example, if you want to search for a line that begins with the string welcome, you can use regex ^ welcome, See the following command.

[[email protected] ~]# grep ^welcome file.txt  # Searching by Matching anchor at start of the Line
welcome to itsmarttricks.com

If you want to search for a line that ends with the string com then you can use regex com $, See the following command.

[[email protected] ~]# grep com$ file.txt   # Searching by matching anchor at end of the Line
welcome to itsmarttricks.com
i love itsmarttricks.com

Check the installed grep package

You can check the version of the installed grep package with the grep command with argument -V,

[[email protected] ~]# grep -V   # Checking for Installed grep package version
GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

For more information command options using the following command on your Linux system.

[[email protected] ~]# man grep  # For more grep command options

READ ALSO – Best Linux Du Command With Examples

That’s all. In this article, we explained best Linux Grep command Examples. I hope you like this article. If you like this article, just share it. If you have any questions about this article, please comment.

Sidebar