egrep

A collection of Unix/Linux ‘grep’ command examples

Linux grep commands FAQ: Can you share some Linux/Unix grep command examples?

Sure. The name grep means "general regular expression parser", but you can think of the grep command as a "search" command for Unix and Linux systems: it's used to search for text strings and more-complicated "regular expressions" within one or more files.

I think it's easiest to learn how to use the grep command by showing examples, so let's dive right in.

Linux: Recursive file searching with grep -r (like grep + find)

Linux grep FAQ: How can I perform a recursive search with the grep command in Linux?

Solution: find + grep

For years I always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match a grep pattern:

find . -type f -exec grep -l 'alvin' {} \;

This command can be read as, “Search all files in all subdirectories of the current directory for the string ‘alvin’, and print the filenames that contain this pattern.” It’s an extremely powerful approach for recursively searching files in all subdirectories that match the pattern I specify.

An egrep example with multiple regular expressions

Summary: How to use the Linux egrep command with multiple regular expressions (regex patterns).

As a quick note here today, I just used the Linux egrep command to perform a case-insensitive search on multiple regular expressions (regex patterns). Really, what I did was a little more complicated:

locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'

As you can see from that command, I did this:

Use zgrep to grep a gzip (gz) file

Linux zgrep FAQ: How do I use the Linux zgrep command? (Or, How do I grep a GZ file?)

Linux users quickly learn how to use the Linux grep command on plain text files, but it takes a little longer to really you can grep gzip (gz) files as well. Here's how.

grep reverse: how to reverse the meaning of a `grep` search

Problem

You need to reverse the meaning of a search you're performing with the grep command. For instance, you've been searching for pepperoni pizza orders like this:

grep pepperoni pizza-orders.txt

and now you need to find all orders that don't have pepperoni.

Solution

Just add the -v switch to your grep search command, like this: