How to Exclude Patterns, Files, and Directories with Grep

How to Exclude Patterns, Files, and Directories with Grep

Quick Links

Since 1974, the Linux grep command has been helping people find strings in files. But sometimes grep is just too thorough. Here are several ways to tell grep to ignore different things.

The grep Command

The grep command searches text files looking for strings that match the search patterns you provide on the command line. The power of grep lies in its use of regular expressions. These let you describe what you're looking for, rather than have to explicitly define it.

The birth of grep pre-dates Linux. it was developed in the early 1970s on Unix. It takes its name from the g/re/p key sequence in the

ed 

line editor (incidentally, pronounced "ee-dee"). This stood for global, regular express search, print matching lines.

grep is famously---perhaps, notoriously---thorough and single-minded. Sometimes it'll search files or directories you'd rather it didn't waste its time on, because the results can leave you unable to see the wood for the trees.

Related: How to Use the grep Command on Linux

Of course, there are ways to reign grep in. You can tell it to ignore patterns, files, and directories so that grep completes its searches faster, and you're not swamped with meaningless false positives.

Excluding Patterns

To search with grep you can pipe input to it from some other process such as

cat 

, or you can provide a filename as the last command line parameter.

We're using a short file that contains the text of the poem Jabberwocky, by Lewis Carroll. In these two examples, we're searching for lines that match the search term "Jabberwock."

cat jabberwocky.txt | grep "Jabberwock"
grep "Jabberwock" jabberwocky.text

The lines that contain matches to the search clue are listed for us, with the matching element in each line highlighted in red. That's straightforward searching. But what if we want to exclude lines that contain the word "Jabberwock" and print the rest?

We can accomplish that with the -v (invert match) option. This lists the lines that don't match the search term.

grep -v "Jabberwock" jabberwocky.text

The lines that don't contain "Jabberwock" are listed to the terminal window.

We can exclude as many terms as we wish. Let's filter out any lines that contain "Jabberwock" and any lines that contain "and." To achieve this we'll use the -e (expression) option. We need to use it for each search pattern we're using.

grep -v -e "Jabberwock" -e "and" jabberwocky.txt

There's a corresponding drop in the number of lines in the output.

If we use the -E (extended regexes) option, we can combine the search patterns with "|", which in this context doesn't indicate a pipe, it's the logical OR operator.

grep -Ev "Jabberwock|and" jabberwocky.txt

We get exactly the same output as we did with the previous, longer-winded command.

The format of the command is the same if you want to use a regex pattern instead of an explicit search clue. This command will exclude all lines that start with any letter in the set of "ACHT."

grep -Ev "^ACHT" jabberwocky.txt

To see lines that contain a pattern but which also don't contain another pattern, we can pipe grep into grep . We'll search for all lines that contain the word "Jabberwock" and then filter out any lines that also contain the word "slain."

grep "Jabberwock" jabberwocky.txt | grep -v "slain"
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.