The ‘grep’ command is used to look for a specified file for patterns defined by the user.
The grep command is used to search text. It searches the specified file for lines containing a match to the specified words or strings. It is among the most useful commands in Linux and Unix-like systems.
Fundamentally ‘grep’ enables you to input a pattern of text and then it searches for this pattern within the text which you provide in it. It returns all the lines which have the specified pattern or string in them.
The grep is a command-line tool to search for regular expressions. The grep will print the fitting line to the output and with the –color option you can highlight the matching strings.
So, what are regular expressions?
A regular expression (regex) defines a search pattern for strings. The search pattern can be anything from a simple character, a fixed string, or a complex expression containing special characters describing the pattern.
Let’s see how to use grep on a Linux or Unix like system.
Syntax for using the grep command
grep [-options] pattern filename
The general syntax for using the grep command is providing a word and specifying a file name. For example:
$ grep ‘word_to_search’ filename
You may also specify multiple files:
$ grep ‘text’ file1 file2 file3
Multiple words search
$ grep ‘word1 word2’ filename
The section below shows a few examples of grep command with various options and a little detail wherever required.
An example of searching a file
grep testUsr /etc/passwd
Searching a word in a specified file example
For finding a word in the specified file, you may use a grep command as follows:
$ grep Mango fruits.txt
You may also use fgrep command as well:
$ fgrep Mango fruits.txt
How to use grep recursively
Use the –r option in grep command for searching recursively. The example command below will search in all files for a given string:
$ grep -r "some_text" /etc/
How to find all files with a specific extension
The grep can be extremely helpful for filtering from stdout. By way of instance, let’s say you have an entire folder full of audio files in a whole lot of different formats. You are searching for all the *.mp3 files from the person Haynes, but you do not need any of the tutorial audio files. With a find command with a couple of grep pipes will do the trick:
$ find . -name "*.mp3" | grep -i Haynes | grep -vi "tutorial"
Using –w option to search for specified word only
When you search for “text”, grep will fit textdemo, text123, textmore etc.. You can force the grep command to choose only those lines containing matches that form whole words i.e. match just “text” word:
$ grep -w "boo" file
How to use grep to search two Distinct words
An example of searching two words:
$ egrep -w 'string1|string2' /path/to/file
Count Number of Matches example
# ifconfig | grep -c inet6
How to perform case insensitive search
We can force grep to ignore case distinctions in data and patterns. For example, searching for “text” matches Text, texT, TEXT etc.
$ grep -i 'text' /path/to/file
Search Files by Given String
The –n option for grep is quite helpful when debugging files through compile errors. It displays the line number from the file of the specified search string:
$ grep –n "main" setup.py
The –v option example for invert match
If you want to excludet the given word then use the –v option in grep command. For example:
$ grep -v test /path/to/file $ grep -v '^test' /etc/passwd
Search a string in Gzipped Files using zgrep
The zgrep, a derivative of grep command, can be used for searching in gzipped files.
It takes the same options as grep and can be used in the same manner:
$ zgrep –i error /var/log/syslog.2.gz
UNIX / Linux pipes
The grep command frequently used with shell pipes. In this example, show the title of the hard disk devices:
$ dmesg | egrep '(s|h)d[a-z]'
What are pipes?
A casing pipe is a way to connect the output of one program into the input of another program with no temporary file.
Screen cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'