How to search file contents with grep - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

How to search file contents with grep
=====================================

### [\#CommandLine](https://rocketeersapp.com/knowledge/command-line)

Search inside files with grep — recursive search, case-insensitive matching, line numbers, context, regex and piping output from other commands into grep.

 Published by [Mark van Eijk](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [Searching inside files with grep](#content-searching-inside-files-with-grep)
2. [Search recursively](#content-search-recursively)
3. [Case-insensitive matching](#content-case-insensitive-matching)
4. [Show line numbers](#content-show-line-numbers)
5. [Match whole words and invert](#content-match-whole-words-and-invert)
6. [Count matches](#content-count-matches)
7. [Show surrounding context](#content-show-surrounding-context)
8. [Using regular expressions](#content-using-regular-expressions)
9. [Piping into grep](#content-piping-into-grep)

[\#](#content-searching-inside-files-with-grep "Permalink")Searching inside files with grep
-------------------------------------------------------------------------------------------

While [find](/find-files-linux-command) locates files by their name or metadata, `grep` searches what's *inside* them. It scans for lines matching a pattern and prints them. The basic form is `grep 'pattern' file`:

 ```
grep 'database' config/app.php

```

[\#](#content-search-recursively "Permalink")Search recursively
---------------------------------------------------------------

To search every file under a directory, add `-r` (recursive). This is the one I use most:

 ```
grep -r 'API_KEY' .

```

[\#](#content-case-insensitive-matching "Permalink")Case-insensitive matching
-----------------------------------------------------------------------------

By default grep is case-sensitive. Add `-i` to ignore case, so `Error`, `error` and `ERROR` all match:

 ```
grep -ri 'todo' src/

```

[\#](#content-show-line-numbers "Permalink")Show line numbers
-------------------------------------------------------------

Add `-n` to prefix each match with its line number, which makes jumping to it in an editor much faster:

 ```
grep -n 'function' app.js

```

[\#](#content-match-whole-words-and-invert "Permalink")Match whole words and invert
-----------------------------------------------------------------------------------

Use `-w` to match a whole word only, so searching `log` won't also match `login` or `catalog`:

 ```
grep -w 'log' app.php

```

Flip the logic with `-v` to print lines that *don't* match — useful for filtering noise out of a file:

 ```
grep -v '^#' config.ini

```

That example drops every commented line (those starting with `#`).

[\#](#content-count-matches "Permalink")Count matches
-----------------------------------------------------

When you only care how many times something appears, `-c` prints the count of matching lines instead of the lines themselves:

 ```
grep -c 'POST' access.log

```

[\#](#content-show-surrounding-context "Permalink")Show surrounding context
---------------------------------------------------------------------------

Sometimes the matching line alone isn't enough. Use `-A` for lines after, `-B` for before, and `-C` for both:

 ```
grep -C 3 'Exception' storage/logs/laravel.log

```

This prints each match with 3 lines on either side.

[\#](#content-using-regular-expressions "Permalink")Using regular expressions
-----------------------------------------------------------------------------

The pattern is a regular expression, so you can match more than literal text. Add `-E` for extended regex syntax like alternation and `+`:

 ```
grep -E 'warning|error|critical' app.log

```

This finds any line containing one of those three words.

[\#](#content-piping-into-grep "Permalink")Piping into grep
-----------------------------------------------------------

grep really shines at the end of a pipe, filtering the output of another command. This is everyday glue on the command line:

 ```
ps aux | grep nginx
git log --oneline | grep 'fix'

```

Combine these flags freely — for example `grep -rin 'token' .` gives you a recursive, case-insensitive search with line numbers — and grep becomes one of the most useful tools in your kit.

### Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#CommandLine](https://rocketeersapp.com/knowledge/command-line)

- [Argument list too long (Bash: /bin/rm)](https://rocketeersapp.com/knowledge/argument-list-too-long)
- [How to install Composer packages locally](https://rocketeersapp.com/knowledge/install-composer-packages-locally)
- [How to send GET and POST requests with curl](https://rocketeersapp.com/knowledge/curl-post-get-api-requests)
- [Essential Linux command line basics for developers](https://rocketeersapp.com/knowledge/linux-command-line-basics)
- [The complete guide to the curl command](https://rocketeersapp.com/knowledge/curl-command-complete-guide)
- [Getting started with the AWS CLI](https://rocketeersapp.com/knowledge/getting-started-aws-cli)

 [View all 21 articles →](https://rocketeersapp.com/knowledge/command-line)
