How to Use the grep Command in Linux

Updated on August 16, 2024
 How to Use the grep Command in Linux header image

Introduction

The grep command in Linux is a sorting utility used to search for text patterns in files. It's also known as global regular expression print (grep) and actively matches patterns in output file types such as configurations, text, or application code.

This article explains how to use the grep command in Linux to search and manipulate text in files.

grep Command Syntax

Below is the grep command syntax:

console
$ grep [options] pattern [file...]

In the above command, pattern is the target text or regular expression to look up while [file...] specifies one or more files to search.

grep Command Options

Option Description
-i Ignore case distinctions in the pattern.
-v Invert the match, displaying lines that do not match a pattern.
-c Count the number of matching lines.
-l List filenames with matching lines.
-n Display line numbers with matching lines.
-r Recursively search directories.
-w Match whole words only.
-x Match whole lines only.
-A Display N lines of context after the match.
-B Display N lines of context before the match.
-C Display N lines of context around the match.

Practical Examples of the grep Command

  1. Perform a basic search.

    console
    $ grep "Vultr" file.txt
    

    The above command searches for the string Vultr in file.txt and displays all matching results in the file.

    Output:

    grep command

  2. Ignore case sensitivity.

    console
    $ grep -i "Vultr" file.txt
    

    The above command looks up for the text Vultr and displays all results that match any case. For example, text such as vultr, vULTR, and VULTR match the search.

    Output:

    grep case-insensitive manner

  3. Invert match and display all lines that do not include a specific string.

    console
    $ grep -v "Vultr" file.txt
    

    The above command displays all lines that don't include the string Vultr.

    Output:

    invert match

  4. Count lines with matching pattern.

    console
    $ grep -c "Vultr" file.txt
    

    The above command counts the number of lines that include the text Vultr.

    Output:

    grep count

  5. List director files that match a specific filename order.

    console
    $ grep -l "Hello" *.txt
    

    The above command lists all files in the working directory that include the string Hello.

    Output:

    grep filename with match

  6. Show line numbers.

    console
    $ grep -n "Vultr" file.txt
    

    The above command displays all lines that match the string Vultr and the respective line numbers.

    Output:

    grep with line numbers

  7. Perform a recursive search.

    console
    $ grep -r "Hello" /path/to/directory
    

    The above command searches for the string Hello in all files within a specific directory and all sub-directories.

    Output:

    recursive search

  8. Match whole words.

    console
    $ grep -w "Vultr" file.txt
    

    The above command searches for the string Vultr as a whole word in the file file.txt.

    Output:

    match whole word only

Using Regular Expressions with grep

The grep command supports regular expressions (Regex) that describe a sets of strings to search with advanced pattern matching methods.

  1. Search for a string at the beginning of a line.

    console
    $ grep "^Vultr" file.txt
    

    The above command matches and displays all lines that start with the string Vultr.

    Output:

    string at the beginning of a line

  2. Search for a string at the end of a line.

    console
    $ grep "rates.$" file.txt
    

    The above command matches and displays lines that end with the string rates..

    Output:

    string at the end of a line

  3. Search for lines that containing a range of number from between 0 and 9.

    console
    $ grep "[0-9]" file.txt
    

    The above command matches and displays the lines that contain any number in the specified range.

    Output:

    grep digit lines

Advanced Usage Scenarios

  1. Search for multiple patterns.

    console
    $ grep -e "Agrawal" -e "Aggrawal" file.txt
    

    The above command searches for all lines that contain either Agrawal or Aggrawal.

    Output:

    multiple patterns

  2. Combine grep with other commands to filter results.

    console
    $ ps aux | grep "process_name"
    

    The above command searches for all processes and uses grep to filter all running processes with the string process_name.

    Output:

    combine with other command

  3. Use grep with find.

    console
    $ find /path/to/directory -type f -exec grep "example" {} +
    

    The above command searches for the string example in the find command output.

    Output:

    grep with find command

  4. Display lines with context.

    console
    $ grep -C 3 "Vultr" file.txt
    

    The above command displays three lines of context on each matching line.

    Output:

    grep with context

  5. Search compressed files.

    console
    $ zgrep "pattern" file.gz
    

    The above command searches for the string pattern in the compressed file.gz file.

    Output:

    grep in compressed files

  6. Exclude specific files from search.

    console
    $ grep --exclude="*.log" -r "pattern" /path/to/directory
    

    The above command recursively searches for pattern but excludes files with the .log extension.

    Output:

    exclude specific files

Conclusion

You have used the grep command to efficiently search and manipulate textual data in files and directories. For more command options, the man grep command to view the grep manual page.