How To Use Linux Grep Command To Find Strings

Linux Grep

Linux and UNIX-like systems, all system configuration information is stored and manipulated in plain text form. The grep tool allows to search of text, such as a file or output of the command, for a term or pattern matching regular expressions. Grep is used to search text for patterns specified by the user. It is one of the most useful and powerful commands on Linux and UNIX like operating system. When grep finds match in a line, it copies in to the screen that is stdout. Using grep command you can replace the string in given file. Grep provides a number of additional options that, if specified , force the program to output the context for each match.


grep [ option(s) ] pattern [file(s) ]

Example 1 : Basic Grep Command

The basic usage of grep command is to search for a specific string in the specified file as shown below.

[[email protected] ~]# grep root /etc/passwd

Above example grep searches the /etc/passwd file for root string and redirected to stdout.


-i This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “hal”, “HAL” and “The” case insensitively as shown below.

Even you can use Line and, anchors, character classes, wild cards,

[[email protected] ~]# grep -i HAL /etc/passwd
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

Example 2 : Searching string in a given file

[[email protected] ~]# grep "root" /etc/passwd

Example 3 : Searching string in a multiple files

[[email protected] ~]# grep "root" /etc/passwd /etc/group

Example 4 : Regular Expressions in files

? The preceding item is optional and matched at most once.
• * The preceding item will be matched zero or more times.
• + The preceding item will be matched one or more times.
• {n} The preceding item is matched exactly n times.
• {n,} The preceding item is matched n or more times.
• {,m} The preceding item is matched at most m times.
• {n,m} The preceding item is matched at least n times, but not more than m times.

[[email protected] ~]# grep " *oot" /etc/passwd

Example 5 : Searching full word using grep –w

[[email protected] ~]# grep -iw "samba" /etc/samba/smb.conf
# This is the main Samba configuration file. You should read the
# here. Samba has a huge number of configurable options (perhaps too
# For a step to step guide on installing, configuring and using samba,
# read the Samba-HOWTO-Collection. This may be obtained from:

Example 6 : Grep with option -A

-A option which displays the N lines after the string match.

[[email protected] ~]# grep -A 2 "root" /etc/passwd


Example: 6 : -B option which displays the N lines before the string match

[[email protected] ~]# grep -B 2 "root" /etc/passwd

Example 7 : -E option can be used search multiple strings

[[email protected] ~]# ls | grep -E “li|ovo”
[[email protected] ~]#

Or we can use

[[email protected] ~]# ls | grep ‘li\|ovo’
[[email protected] ~]#

Example 8 : Below grep command will search string in all files except syslog.log

[[email protected] ~]#grep -r string * | grep -v ‘/\syslog.log/’

Bobbin Zachariah 7:25 pm

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.