Linux and UNIX-like systems, all system configuration information is stored and manipulated in plain text form. So searching text would come very common task for editing and log analysis.
The grep command allows searching for a text or string in a file or from output console of a command, for a term or pattern matching regular expressions. When grep finds match in a line, it copies results into the screen ie stdout.
In this tutorial we learn how to use grep command with practical examples.
Lets check grep command syntax before we start explaining its usage.
grep [OPTIONS] PATTERN [FILE...]
We will discuss in detail most common grep options and patterns we can use for searching.
How search a string in a file
In the below example grep searches the /etc/passwd file for the string 'root' and redirected output to stdout.
$ grep "root" /etc/passwd
Search text from multiple files
You can search specific text from multiple files using the following command:
$ grep "root" /etc/passwd /etc/group
Grep case insensitive search
-i searches for the given string/pattern case insensitively.
In following examples grep matches all the words such as “hal”, “HAL” case insensitively:
$ grep -i HAL /etc/passwd
Recursive search is achieved by using
-r option that will search Recursively all file skipping symbolic links. If you want to include symlinks use
In the following example, grep command will search for the string 'ngnix' in all files inside
$ grep -r nginx /var
Search for a String in Command Output (stdout)
You can use pipe the output for a command and grep for the pattern from its output.
In the following example I am searching for a file with name 'backup' using the ls command
$ ls | grep backup
Here i am doing multiple pipe and searching for the 'docker' and 'apache' process from ps command.
$ ps -ef | grep docker | grep apache
Regular Expressions in files
Regular Expressions is a pattern to match for each input line. Basic and Extended are the two regular expression used by grep command.
Basic regular expression
By default, grep interprets the pattern as a basic regular expression.
The ^ (caret) symbol - The pattern following it must occur at the beginning of each line. In the following example, the string ^welcome will match only if it occurs at the very beginning of a line.
$ grep “^welcome” filename
Extended Regular Expression
-E ( or --extended-regexp) option if you want grep to understand it as an extended regular expression.
The following command will extract all links from an file
$ grep –Eoi '<a[^>]+>.*</a>' filename
o : By default, grep prints entire line which contains the search pattern. This option instructs grep command to print only the matching words instead of entire line.
i : This option ask grep command to ignore the case while matching the pattern.
Search a full word using grep
-w (word-regexp) flag for grep will make the given expression match only whole words.
$ 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:
Display lines after the string match
-A option which displays the
N lines after the string match.
$ grep -A 2 "root" /etc/passwd
Display lines before the string match
We can use
-B option to display N lines before the string for a given file
$ grep -B 2 "root" /etc/passwd
Search multiple strings
We can search multiple patterns or strings using OR operator
-E (extended-regexp) controls whether you need to escape certain special characters.
-e is strictly the flag for indicating the pattern you want to match against.
Example 1: using -E option
$ ls | grep -E “li|ovo”
$ grep -E "foo|bar" *.txt
Example 2: without -E
$ ls | grep ‘li\|ovo’
$ grep "foo\|soul\|bar" *.txt
Exclude specific file from searching
You can use -v option to ignore searching. The following command will search the string 'error' in all files except 'syslog.log'
$ grep -r error * | grep -v ‘/\syslog.log/’
In this tutorial we learned how to use grep command to find text in files. I hope your enjoyed reading this and please leave your suggestions in the below comment section.