Grep Command in Linux - UNIX With Complete Examples - Nixcraft
Grep Command in Linux - UNIX With Complete Examples - Nixcraft
nixCraft
How do I use grep command on Linux or Apple macOS/OS X? How can I use grep command on Unix operating
systems? Can you give me a simple examples of the grep command?
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. Search any line that contains the word in filename on Linux: grep 'word' filename
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
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/amp/?__twitter_impression=true 1/7
5/3/2020 grep Command In Linux / UNIX with complete examples - nixCraft
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:
fgrep California address.txt
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:
fgrep -w California address.txt
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:
grep -i "boo" /etc/passwd
The last grep -i "boo" /etc/passwd can run as follows using the cat command too:
cat /etc/passwd | grep -i "boo"
OR
$ grep -R "192.168.1.5" /etc/
Sample outputs:
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:
$ grep -h -R "192.168.1.5" /etc/
OR
$ grep -hR "192.168.1.5" /etc/
Sample outputs:
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/amp/?__twitter_impression=true 2/7
5/3/2020 grep Command In Linux / UNIX with complete examples - nixCraft
# ms-wins 192.168.1.50
# ms-wins 192.168.1.51
addresses1=192.168.1.5;24;192.168.1.2;
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:
$ 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:
grep -A NUM "string" /pth/to/file
Here is a sample shell script that fetches the Linux kernel download urls:
.......
...
_out="/tmp/out.$$"
#######################
#######################
gpgurl="${url/tar.xz/tar.sign}"
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/amp/?__twitter_impression=true 3/7
5/3/2020 grep Command In Linux / UNIX with complete examples - nixCraft
echo "* Downloading the Linux kernel (new version) ..."
.....
..
grep command often used with shell pipes. In this example, show the name of the hard disk devices:
However, above command can be also used as follows without shell pipe:
model : 30
model : 30
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:
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/amp/?__twitter_impression=true 4/7
5/3/2020 grep Command In Linux / UNIX with complete examples - nixCraft
Use the -l option to list file name whose contents mention main():
In conclusion, the --color option increase readblity. For example, the GREP_COLOR environment variable and the grep --color=always can
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/amp/?__twitter_impression=true 5/7
5/3/2020 grep Command In Linux / UNIX with complete examples - nixCraft
In addition, to default red color now we can define colors using GREP_COLOR shell variable. The differnt color helps us massivly with v
Conclusion
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
If you enjoyed the grep tutorial, then you might like to read our “Regular Expressions in Grep” tutorial.
This entry is 1 of 7 in the Linux / UNIX grep Command Tutorial series. Keep reading the rest of the series:
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/amp/?__twitter_impression=true 6/7
5/3/2020 grep Command In Linux / UNIX with complete examples - nixCraft
View Comments
nixCraft
Back to top
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/amp/?__twitter_impression=true 7/7