In this article, we are going to learn how to use Linux grep command in Linux. GREP stands for Global Regular Expression Print. Linux grep command is used to search in Linux and Unix. Here we are going to learn some very important and useful Linux Grep command examples.
Best Linux Grep Command Examples:
Syntax to Use grep command :
grep [Options] [String for Search] [File Path]
Search for a String using Linux grep command
I have a file named file.txt with some text’s as shown in the output below.
[root@localhost ~]# 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 search for a string i.e. Linux from file file.txt.
[root@localhost ~]# grep linux file.txt # Search for a String linux is a open source operating system.
Color the Searched String
Search for a string by highlighting in color using grep command with option – -color.
[root@localhost ~]# grep --color linux file.txt # Search for string by Highlighting it with color linux is a open source operating system.
Let’s take one more example. Here I am searching for user itsmarttricks in /etc/passwd file using grep command.
[root@localhost ~]# cat /etc/passwd | grep --color itsmarttricks # Searching for a User using grep Command itsmarttricks:x:501:501::/home/itsmarttricks:/bin/bash
You also can search for an Installed Package. Here I am searching if vsftpd package is installed or not.
[root@localhost ~]# rpm -qa | grep vsftpd # Searching for a Installed Package vsftpd-2.2.2-24.el6.x86_64
Search for Case Insensitive strings using grep command
We can search Case Insensitive strings using grep command with argument -i. Here I am searching for string Linux and you will able to notice on the output below that I able to search all the strings whether it contains Uppercase or Lowercase.
[root@localhost ~]# 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.
Search a String in Multiple Files using grep command
You can search for a string from multiple files. Here I have a directory named data which contains two files i.e. a.txt and b.txt. and I am going to search for string Linux from both the files. Refer to the sample output below.
You can see in the output below we are able to search for the string Linux from both the files and the output show’s the searched string with the file name from which it searched.
[root@localhost data]# pwd /root/data [root@localhost data]# ls a.txt b.txt [root@localhost 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 searching for strings i.e. Linux, itsmarttricks, Open from file.txt.
[root@localhost ~]# 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 Files using Wild Card
We can use a wildcard to search with the grep command. For example, here I am searching for string Linux from all available text files. Refer to the command below.
Syntax : grep -i [string] [path of .txt files]
[root@localhost data]# ls a.txt b.txt c.txt [root@localhost 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.
Search for Invert Match using grep command
Another nice feature we have in the grep command is inverted search. Invert search means It will search only those lines which don’t contain the searched string. For example, here I am searching for the string itsmarttricks, In that case, grep will search only those lines which are don’t contains itsmarttricks string.
[root@localhost ~]# 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 [root@localhost ~]# 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.
Search by Matching Number Regex using grep Command
we can use grep command to Search by using regex. For example, here I am using regex 0-9. means grep will search all those lines which contains any one number from 0 to 9. Refer to the command below.
[root@localhost ~]# grep '[0-9]' test.txt # Searching using Regex 1 2 3 4 5 20 grep command with examples 6 7 8 9 0
Like the number Regex, we can also set Regex for letters like a-z. Refer to the command below.
[root@localhost ~]# grep '[a-z]' file.txt # Searching using Regex Linux is a Open Source Operating System. linux is a open source operating system. [root@localhost ~]# 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 using grep command
To search for a string on a directory and also on all sub-directory contained by that directory recursively. Here I have a directory named data which contains some txt files with some content and also have a sub-directory named testing (i.e. /data/testing) which also contains some txt files.
So here I am going to search for the string Linux in directory data and also in all subdirectories contained by it using grep command with argument -r. Refer to the command below.
[root@localhost ~]# cd data/ [root@localhost data]# pwd /root/data [root@localhost data]# ls test1.html test1.txt test2.html test2.txt test3.html test3.txt testing [root@localhost data]# cd testing/ [root@localhost testing]# pwd /root/data/testing [root@localhost testing]# ls test1.html test1.txt test2.html test2.txt test3.html test3.txt [root@localhost ~]# 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
Print the Line Number of Searched String using grep command
grep command with argument -n will show you the line number where the string is available in the file. Here I am searching for the user itsmarttricks in /etc/passwd file.
As you can see on the sample output below it’s available online number 31 in /etc/passwd file.
[root@localhost ~]# 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
Print the Number of Matches found for searched string
grep command with argument -c will show give you the count of the total number of times the string is repeated in the file.
[root@localhost ~]# grep -i -c open file.txt # Searching for the count of Numeber of times the string is repeated in the File 3 [root@localhost ~]# 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
Search for exact matched String
You can search for an exact matched string using grep command with argument -w. Now you might think that what is an exact matching string. Suppose you are searching for the string Linux then grep searches for all that word that contains Linux like itsmarttricks, linuxbooks, linuxtraining, and so on. So here exact match means it will only search if the word is linux and not any other.
[root@localhost ~]# grep -w "welcome" file.txt # Searching for Exact matched String welcome to itsmarttricks.com
Search for an exact matched Line
Like exact matched string you can search for exactly matched line using grep command with argument -x. Refer to the command below.
[root@localhost ~]# grep -x "linux is a open source operating system." file.txt # Searching for Exact matched Line linux is a open source operating system. [root@localhost ~]# grep -x "linux is a open source operating" file.txt
Search by Anchor Match
You can search by matching the anchor at the start and end of the line. For example, if you want to search for some line that starts with the string welcome then you can use regex ^welcome. Refer to the command below.
[root@localhost ~]# grep ^welcome file.txt # Searching by Matching anchor at start of the Line welcome to itsmarttricks.com
If you want to search for some line that ends with the string com then you can use regex com$. Refer to the command below.
[root@localhost ~]# grep com$ file.txt # Searching by matching anchor at end of the Line welcome to itsmarttricks.com i love itsmarttricks.com
Check the Version installed grep Package
You can check the version of the installed grep package using grep command with argument -V.
[root@localhost ~]# 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 <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
For more grep, command options use the below command on your Linux system.
[root@localhost ~]# man grep # For more grep command options
Also Read – Best Linux Du Command With Examples
That’s all, In this article, we have explained the Best Linux Grep Command Examples. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.