How to use grep command

The grep command which stands for “global regular expression print” is one of the most powerful and commonly used commands in Linux.

Grep searches one or more input files for lines that match a given pattern and writes each matching line to standard output. If no files are specified, grep reads from the standard input which is usually the output of another command.
In this tutorial, we will show you how to use the grep command through practical examples and detailed explanations of the most common GNU grep options.

Basic Syntax for grep Command

Before going into how to use the Grep command, let’s start by reviewing the basic syntax.


OPTIONS: Grep options are used to change the behavior of the command.

  • -i : Used for ignoring matching case.
  • -c: It prints count of lines matching.
  • -l : Displays list of filenames.
  • -n : Number of matched lines and their line numbers.
  • -v : Number of lines that do not match the pattern.
  • -w : Match the whole word

PATTERN: Provide the search pattern.
FILE: Input file names.

1. How to use Grep to Search for a String in File

Grep command mostly used for searching string in the file. You can also search string inside file case sensitively.
For example, to display the lines from the /etc/passwd file containing the string bash you can use the following command:

grep bash /etc/passwd

The output should look something like this:

[email protected]:~$ grep bash /etc/passwd
rasho:x:1000:1000:Radenko Bogdanovic,,,:/home/rasho:/bin/bash

If string you want to search includes spaces then enclose them inside single or double quotes. In the below example, it will search Gnome Display Manage in /etc/passwd file.

2. Display Number Of Matching Lines

You can display number of matching lines using grep command with -c option. Run following command to find count of string bash in /etc/passwd file:

[email protected]:~$ grep -c bash /etc/passwd

3. Grep Invert Match (Exclude)

Invert match (-vor --invert-match) option is used to invert the grep output. The command will display the lines which do not match the given pattern. For example to display the lines from the /etc/passwd file that do not contain the string nologin you can use the following command:

grep -v nologin /etc/passwd

The output should look something like this:

[email protected]:~$ grep -v nologin /etc/passwd
lightdm:x:111:117:Light Display Manager:/var/lib/lightdm:/bin/false
speech-dispatcher:x:117:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:119:7:HPLIP system user,,,:/var/run/hplip:/bin/false
rasho:x:1000:1000:Radenko Bogdanovic,,,:/home/rasho:/bin/bash

4. Grep Recursive Search

To recursively search for a pattern use the, use the -r option (or --recursive). This will search through all files in the specified directory, skipping the symlinks that are encountered recursively. To follow all symbolic links, use the -R option (or --dereference-recursive).
In below example string “Rasho” will be searched in all files inside Documents directory:

grep -r Rasho /Documents/

By using option -R you can also search symbolic link files inside directories:

grep -R Rasho /Documents/

To search string in all directories you can run the following command:

grep -r "Rasho" *

5. Search String in Standard Output

If you want to search string inside a command output, you can do so by combining grep command with another command or commands.
To find the string inet6 inside command output of ifconfig run following command:

ifconfig | grep inet6

Example output:

[email protected]:~$ ifconfig |grep inet6
        inet6 fe80::ca1c:8074:f984:5668  prefixlen 64  scopeid 0x20<link>
        inet6 ::1  prefixlen 128  scopeid 0x10<host>

6. Grep Show Only Filename

To suppress the default grep output and print only the names of files containing the matched pattern you can use the -l ( or --files-with-matches) option.
For example to search through all files ending with .conf in the current working directory and print only the names of files containing the string type:

grep -l *.conf

The output will look something like this:

[email protected]:~$ grep -l *.conf

The -l option is usually used in combination with the recursive option -R:

grep -Rl /tmp

7. Search Exact Matching Words

You can search exact matching words inside the file by using option -w or --files-with-matches with grep command.
By using the following command you will only get lines which contain exact word Rasho inside the file:

grep -w Rasho names.txt

8. Show Number of Lines Matching

To display the count of lines matching inside a file you can use option -c with grep command.
In the example below, we are counting the number of accounts that have /usr/bin/zsh as a shell.

grep -c '/usr/bin/zsh' /etc/passwd

9. Grep Multiple Strings (Patterns)

Two or more search patterns can be joined by the OR operator |.
By default, Grep interprets the pattern as a basic regular expression where the meta-characters such as | lose their special meaning and their backslashed versions must be used.
In the example below we are searching all occurrences of the words fatal, error and critical in the Nginx log error file:

grep 'fatal\|error\|critical' /var/log/nginx/error.log

If you use the extended regular expression option -E (or --extended-regexp) then the operator | should not be escaped, as shown below:

grep -E 'fatal|error|critical' /var/log/nginx/error.log

10. Grep Print Lines Before a Match

To print a specific number of lines before matching lines, use the -B ( or --before-context) option.
For example, to display 5 lines of leading context before matching lines you can use the following command:

grep -B 5 root /etc/passwd

11. Grep Print Lines After a Match

To print a specific number of lines after matching lines, use the -A ( or --after-context) option.
For example to display 5 lines of trailing context after matching lines you can use the following command:

grep -A 5 root /etc/passwd


You have successfully learned how to use grep command in Linux. If you have any queries regarding this then please don’t forget to comment below.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.