# grep command

### The Linux `grep` command <a href="#the-linux-grep-command" id="the-linux-grep-command"></a>

The `grep` command is a very useful tool. When you master it, it will help you tremendously in your day to day coding.

> If you're wondering, `grep` stands for *global regular expression print*.

You can use `grep` to search in files, or combine it with pipes to filter the output of another command.

For example here's how we can find the occurences of the `document.getElementById` line in the `index.md` file:

```bash
grep -n document.getElementById index.md
```

<img src="https://www.freecodecamp.org/news/content/images/2020/10/Screen-Shot-2020-09-04-at-09.42.10.png" alt="Screen-Shot-2020-09-04-at-09.42.10" height="314" width="969">

Using the `-n` option it will show the line numbers:

```bash
grep -n document.getElementById index.md
```

<img src="https://www.freecodecamp.org/news/content/images/2020/10/Screen-Shot-2020-09-04-at-09.47.04.png" alt="Screen-Shot-2020-09-04-at-09.47.04" height="439" width="848">

One very useful thing is to tell grep to print 2 lines before and 2 lines after the matched line to give you more context. That's done using the `-C` option, which accepts a number of lines:

```bash
grep -nC 2 document.getElementById index.md
```

<img src="https://www.freecodecamp.org/news/content/images/2020/10/Screen-Shot-2020-09-04-at-09.44.35.png" alt="Screen-Shot-2020-09-04-at-09.44.35" height="539" width="958">

Search is case sensitive by default. Use the `-i` flag to make it insensitive.

As mentioned, you can use grep to filter the output of another command. We can replicate the same functionality as above using:

```bash
less index.md | grep -n document.getElementById
```

<img src="https://www.freecodecamp.org/news/content/images/2020/10/Screen-Shot-2020-09-04-at-09.43.15.png" alt="Screen-Shot-2020-09-04-at-09.43.15" height="314" width="969">

The search string can be a regular expression, and this makes `grep` very powerful.

Another thing you might find very useful is to invert the result, excluding the lines that match a particular string, using the `-v` option:

<img src="https://www.freecodecamp.org/news/content/images/2020/10/Screen-Shot-2020-09-04-at-09.42.04.png" alt="Screen-Shot-2020-09-04-at-09.42.04" height="314" width="969">


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kgk.gitbook.io/resources/resources/linux/commands/grep-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
