---
title: " How to Use the grep Command in Linux"
url: https://docs.vultr.com/how-to-use-the-grep-command-in-linux
description: Learn how to use the grep command in Linux to search for text patterns in files. Discover basic syntax, options, and practical examples for efficient text searching.
publish_date: 2024-08-16T14:00:38.262459Z
last_updated: 2023-11-21T20:23:49.337033Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/1889/432be7f7-c021-464b-81c7-2885cde00ea7.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## 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](https://docs.vultr.com/public/doc-assets/1889/9fbaa9f9-d584-4ee9-86f7-98f51c592db0.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/664c94ea-0446-4d6c-bb9f-43ecd979cde0.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/a0ff762e-886b-4b8b-bbd9-12dcf1e35c3d.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/724eec27-2af9-44f4-853d-c593aef98afd.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/eebe6070-62f1-4f68-9467-901484a07c02.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/eed70325-8e6a-454f-ad31-d288f4295f5a.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/9a38b66c-df66-4c41-898e-70fa7433569a.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/624a9f4b-bb95-4362-baa9-b217bdd73bde.gif)

## 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](https://docs.vultr.com/public/doc-assets/1889/2bfebb2e-7c45-4cd0-80ea-41af89e5280b.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/aa52fa09-990d-4256-b1f1-b978eed3d380.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/c915dad3-ec21-40df-9223-0a147669b128.gif)

### 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](https://docs.vultr.com/public/doc-assets/1889/bd2355a7-5db3-4c1c-9916-5ab78dd4f2f3.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/06714bc1-6427-4146-bab2-0ae351750329.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/b379484f-d9fc-463a-bb69-797fe8810f47.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/b7ad76e6-3dbd-4881-a424-fe00286b18e6.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/429b8bee-a8ba-4e18-9b45-e9c70bf38f42.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1889/4a005977-6090-4265-bf23-52a4682cb6d1.gif)

## 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.
