grep command in Linux w/ examples

This grep command guide is a follow-up of my previous 90 Linux Commands frequently used by Linux Sysadmins article. Every week, or as time allows, I will publish articles on around 90 commands geared toward Linux sysadmins and Linux power users. Let’s continue this series with the grep command.

Grep (global regular expression printer) searches through a file for a specific pattern of characters. When it finds a match in a line, grep copies the line to standard output or whatever output you select using options. grep was initially developed for the Unix operating system but eventually made available for all Unix-like systems, such as Linux.

The general syntax of the grep command is:

grep [option...] [patterns] [file...]

Linux grep command examples

grep command in Linux
…grep man page.

To search for a pattern within a file, use the following:

grep "pattern" /path/to/file

To search all files in the current directory, use the following:

grep pattern *

To search for an exact pattern, use the following:

grep -F "pattern" /path/to/file

To search directories recursively, use the following:

grep -r 'hello' /path/to/dir

To search for a whole word, not a part of a word, use:

grep -w 'word' /path/to/file

To perform a case insens­itive (ignore case) search, use:

grep -i 'pattern' filename

To display the filename which contains the pattern, use the following:

grep -l 'pattern' /var/log/*

To display x lines after matching pattern, use:

grep -A x 'pattern' filename

To display x lines before the matching pattern, use:

grep -B x 'pattern' filename

To display x lines around the matching pattern, use:

grep -C x 'pattern' filename

To display the matching part of the pattern, use the following:

grep -o 'pattern' filename

To display the line number of the matching pattern, use the following:

grep -n 'pattern' filename

To return all lines which don’t match the pattern, use the following:

grep -v 'warning' /var/log/nginx/error.log

To display the lines starting with ‘er’, use:

grep -e '^er' filename

To display lines with 3 w’s in a row (www), use:

grep -E 'w{3}' filename

Related commands:

  • find – Find files or directories under the given directory tree recursively.
  • pgrep – searches running processes and lists the process IDs which match the selection criteria to stdout.
  • ngrep – grep applied to the network layer.
  • locate – search files in Linux.

Useful links/reference: 

Conclusion

grep helps find patterns within files or the file system hierarchy, so it’s worthwhile to learn its options and syntax.

Tags: ,

Comments from our Members

  1. egrep is one of my most used commands. Super, super useful.

  2. Thanks for the guide, this is super helpful as I try to learn more commands.

    Is it possible to use grep to search many directories for text within a file? If I don’t know where the file is or the name of it, but I know some text within it for example.

  3. Thanks for your useful tips.

    As several of these commands have options not specified by POSIX you could mention that you are using the GNU grep.

  4. That’s true. You are right. I will make the change. Thanks for joining our tech community.

  5. Mat

    I think the ‘find’ command may be your friend. I know you can make find run something recursively through your filesystem, and I’m pretty sure the something can be a shell command/script (like grep).

Stop losing users to slow load times.

Blazing-fast, custom-configured NVMe Linux hosting.

Don't guess if your server is fast enough. Know it. Get a free performance audit of your current setup.

This blog is hosted by StackLinux, our parent company. 99.99% uptime.

Start Your Free Server Audit

You write the code. We'll manage the server.

Fully managed Linux hosting built for absolute speed.

Ditch the server maintenance headaches. Our experts custom-configure high-performance NVMe Linux servers specifically for your needs. Try us out. Your first month is entirely on us.

This blog is hosted by StackLinux, our parent company. 99.99% uptime.

Claim Your 30 Free Days

30 days of NVMe Linux hosting. $0 down.

Risk-free migration and a money-back guarantee.

We are so confident in our high-performance NVMe infrastructure that we'll audit your current server for free, migrate you without downtime, and give you 30 days to see the speed difference yourself.

This blog is hosted by StackLinux, our parent company. 99.99% uptime.

Deploy Free for 30 Days
Top ↑