How To Use grep Command In Linux / UNIX


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.



Did you know?

The name, “grep”, derives from the command used to perform a similar operation, using the Unix/Linux text editor ed:

grep command examples

Common grep command explained with examples in Linux:

  1. grep 'word' filename – Search any line that contains the word in filename on Linux
  2. grep -i 'bar' file1 – A case-insensitive search for the word ‘bar’ in Linux and Unix
  3. grep -R 'foo' . – Search all files in the current directory and in all of its subdirectories in Linux for the word ‘foo’
  4. grep -c 'nixcraft' – Search and display the total number of times that the string ‘nixcraft’ appears in a file named

The grep command syntax in Linux / Unix

The syntax is as follows:

grep ‘word’ filename
grep ‘word’ file1 file2 file3
grep ‘string1 string2’ filename
cat otherfile | grep ‘something’
command | grep ‘something’
command option1 | grep ‘data’
grep –color ‘data’ fileName

How do I use grep to search a file on Linux?

Search /etc/passwd file for boo user, enter:
$ grep boo /etc/passwd
Sample outputs:

You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with the -i option:
$ grep -i "boo" /etc/passwd
grep command examples for Linux and Unix users
The last grep -i "boo" /etc/passwd can run as follows using the cat command too:
cat /etc/passwd | grep -i "boo"

How to use grep recursively

You can search recursively i.e. read all files under each directory for a string “”
$ grep -r "" /etc/
$ grep -R "" /etc/
Sample outputs:

You will see result for 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 "" /etc/
$ grep -hR "" /etc/
Sample outputs:

How to use grep to search words only

When you search for boo, grep will match fooboo, boo123, barfoo35 and more. You can force the grep command to select only those lines containing matches that form whole words i.e. match only boo word:
$ grep -w "boo" file

How to use grep to search 2 different words

Use the egrep command as follows:
$ egrep -w 'word1|word2' /path/to/file

How can I count line when words has been matched

The grep can report the number of times that the pattern has been matched for each file using -c (count) option:
$ grep -c 'word' /path/to/file
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
Sample outputs:

Force grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:
$ grep -v bar /path/to/file

UNIX / Linux pipes and grep command

grep command often used with shell pipes. In this example, show the name of the hard disk devices:
# dmesg | egrep '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i 'Model' /proc/cpuinfo
Sample outputs:

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:
dpkg --list | grep linux-image
yum search php | grep gd
apt search maria | egrep 'server|client'

grep command in Linux with examples
Linux grep commands explained with shell pipes examples

How do I list just the names of matching files?

Use the -l option to list file name whose contents mention main():
$ grep -l 'main' *.c
Finally, you can force grep to display output in colors, enter:
$ grep --color vivek /etc/passwd
Sample outputs:

Grep command in action on Linux and Unix like system
Grep command in action


The grep command in Unix/Linux summary is as follows:

Linux grep command options Description
-i Ignore case distinctions on Linux and Unix
-w Force PATTERN to match only whole words
-v Select non-matching lines
-n Print line number with output lines
-h Suppress the Unix file name prefix on output
-r Search directories recursivly on Linux
-R Just like -r but follow all symlinks
-l Print only names of FILEs with selected lines
-c Print only a count of selected lines per FILE
--color Display matched pattern in colors

If you enjoyed the grep tutorial, then you might like to read our “Regular Expressions in Grep” tutorial. You can view gnu grep man page here.

Facebook Comments

Show More

Leave a Reply

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

Back to top button