BEST LINUX GREP COMMAND EXAMPLES
Introduction
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.
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 on 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 elinuxbook in /etc/passwd file using grep command.
[root@localhost ~]# cat /etc/passwd | grep --color elinuxbook # Searching for a User using grep Command
elinuxbook:x:501:501::/home/elinuxbook:/bin/bash
You also can search for a 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 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.
Also Read :
- BEST LINUX LS COMMAND WITH EXAMPLES
- BEST LINUX ALIAS COMMAND WITH EXAMPLES
- MANAGING USERS AND GROUPS IN LINUX – A COMPLETE GUIDE FOR BEGINNERS
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 the sample output below.
You can see on 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, elinuxbook, Open from file.txt.
[root@localhost ~]# grep -i 'linux\|elinuxbook\|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 elinuxbook.com
i love elinuxbook.com
Search for a String in Multiple Files using Wild Card
We can use wildcard to search with grep command. For example here I am searching for string linux from all available text files. Refer 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 grep command is invert search. Invert search means It will search only those lines which doesn’t contains the searched string. For example here I am searching for the string elinuxbook, In that case grep will search only those lines which is doesn’t contains elinuxbook 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 elinuxbook.com
i love elinuxbook.com
[root@localhost ~]# grep -v elinuxbook 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 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 number Regex we can also set Regex for letters like a-z. Refer 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 is contains some txt files with some content and also have a sub-directory named testing (i.e. /data/testing) which is also contains some txt files.
So Here I am going to search for the string linux in directory data and alsoin all subdirectory contained by it using grep command with argument -r. Refer 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 elinuxbook.com
data/testing/test3.html:welcome to elinuxbook.com
data/testing/test2.html:welcome to elinuxbook.com
data/testing/test1.html:welcome to elinuxbook.com
data/test2.html:welcome to elinuxbook.com
data/test1.html:welcome to elinuxbook.com
Also Read :
- HOW TO CREATE SYMLINK (SYMBOLIC LINK) AND HARDLINK IN LINUX
- COMPLETE UNIX COMMANDS AND BASIC LINUX COMMANDS WITH EXAMPLES FOR BEGINNERS
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 elinuxbook in /etc/passwd file.
As you can see on sample output below its available on line number 31 in /etc/passwd file.
[root@localhost ~]# grep -n elinuxbook /etc/passwd # Searching the Line Number Where the String is Available in the File 31:elinuxbook:x:501:501::/home/elinuxbook:/bin/bash
Print the Number of Matches found for searched string
grep command with argument -c will show give you the count of 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 elinuxbook.com
Search for exact matched String
You can search for exact matched string using grep command with argument -w. Now you might thinking that what is exact matching string. Suppose you are searching for the string linux then grep searches for all that word that contains linux like elinuxbook, 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 elinuxbook.com
Search for a exact matched Line
Like exact matched string you can search for exact matched line using grep command with argument -x. Refer 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 anchor at start and end of the line. For example if you want to search for some line that is starts with the string welcome then you can use regex ^welcome. Refer the command below.
[root@localhost ~]# grep ^welcome file.txt # Searching by Matching anchor at start of the Line
welcome to elinuxbook.com
If you want to search for some line that is ends with the string com then you can use regex com$. Refer the command below.
[root@localhost ~]# grep com$ file.txt # Searching by matching anchor at end of the Line
welcome to elinuxbook.com
i love elinuxbook.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
If you found this article useful then Like Us, Share Us, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.