How To Use Grep Command in Linux-Unix
How To Use Grep Command in Linux-Unix
The grep command is used to search text. It searches the given file for lines
containing a match to the given strings or words. It is one of the most useful
commands on Linux and Unix-like system. Let us see how to use grep on a Linux
or Unix like system.
The grep utilities are a family that includes grep, egrep, and fgrep for searching
duties. For most uses, you need to use fgrep as it the fastest and only look into
strings and words. However, typing grep is easy. Hence, it is a personal choice.
1 of 12
started with grep on Linux, macOS, and Unix:
2. Perform a case-insensitive search for the word ‘bar’ in Linux and Unix:
grep -i 'bar' file1
3. Look for all files in the current directory and in all of its subdirectories in Linux
for the word ‘httpd’:
grep -R 'httpd'
4. Search and display the total number of times that the string ‘nixcraft’ appears
in a file named frontpage.md:
grep -c 'nixcraft' frontpage.md
Syntax
The syntax is as follows:
2 of 12
grep boo /etc/passwd
Sample outputs:
foo:x:1000:1000:boo,,,:/home/boo:/bin/ksh
We can use fgrep/grep to find all the lines of a file that contain a particular word.
For example, to list all the lines of a file named address.txt in the current directory
that contain the word “California”, run:
Please note that the above command also returns lines where “California” is part
of other words, such as “Californication” or “Californian”. Hence pass the -w
option with the grep/fgrep command to get only lines where “California” is
included as a whole word:
You can force grep to ignore word case i.e match boo, Boo, BOO and all other
combination with the -i option. For instance, type the following command:
3 of 12
The last grep -i "boo" /etc/passwd can run as follows using the cat command
too:
OR
Sample outputs:
4 of 12
/etc/ppp/options:# ms-wins 192.168.1.51
/etc/NetworkManager/system-connections/Wired connection 1:
addresses1=192.168.1.5;24;192.168.1.2;
You will see result for 192.168.1.5 on a separate line preceded by the name of
the file (such as /etc/ppp/options) in which it was found. The inclusion of the file
names in the output data can be suppressed by using the -h option as follows:
OR
Sample outputs:
# ms-wins 192.168.1.50
# ms-wins 192.168.1.51
addresses1=192.168.1.5;24;192.168.1.2;
5 of 12
How to use grep to search 2 different words
Use the egrep command as follows:
Ignore case
We can force grep to ignore case distinctions in patterns and data. For example,
when I search for ‘bar’, match ‘BAR’, ‘Bar’, ‘BaR’ and so on:
Pass the -n option to precede each line of output with the number of the line in
the text file from which it was obtained:
6 of 12
$ grep -n 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
1042:rootdoor:x:0:0:rootdoor:/home/rootdoor:/bin/csh
3319:initrootapp:x:0:0:initrootapp:/home/initroot:/bin/ksh
Similarly, display the lines after your matches by passing the -A to the grep:
7 of 12
We can combine those two options to get most meaningful outputs:
Here is a sample shell script that fetches the Linux kernel download urls:
.......
...
_out="/tmp/out.$$"
curl -s https://www.kernel.org/ > "$_out"
#######################
## grep -A used here ##
#######################
url="$(grep -A 2 '<td id="latest_button">' ${_out} |
grep -Eo '(http|https)://[^/"]+.*xz')"
gpgurl="${url/tar.xz/tar.sign}"
notify-send "A new kernel version ($remote) has been released."
echo "* Downloading the Linux kernel (new version) ..."
wget -qc "$url" -O "${dldir}/${file}"
wget -qc "$gpgurl" -O "${dldir}/${gpgurl##*/}"
.....
..
However, above command can be also used as follows without shell pipe:
8 of 12
# grep -i 'Model' /proc/cpuinfo
model : 30
model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz
model : 30
model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz
One of my favorite usage of grep or egrep command to filter the output of the
yum command/dpkg command/apt command/apt-get command:
9 of 12
Linux grep commands explained with shell pipes examples
OR
Colors option
Finally, we can force grep to display output in colors, enter:
10 of 12
Grep command in action
In addition, to default red color now we can define colors using GREP_COLOR
shell variable. The differnt color helps us massivly with visual grepping.
Conclusion
11 of 12
The grep command is a very versatile and many new Linux or Unix users find it
complicated. Hence, I suggest you read the grep man page too. Let us
summarize most import options:
12 of 12