Sep 6, 2023 4 min read

How to Exclude in Grep

Exclude in Grep with our step-by-step tutorial. A grep searches files for matching lines using regex and outputs matches.

Exclude in Grep
Table of Contents

Introduction

Before we begin talking about how to exclude in grep, let's briefly understand – What is grep?

grep is a command-line tool that searches one or more input files for lines that match a regular expression and outputs each match to a standard output.

The name "grep" stands for "Global Regular Expression Print". It allows you to search for specific patterns or strings of characters and display the matching lines. grep supports regular expressions, which gives it powerful search capabilities. It can be used to search within a single file or multiple files, and you can control the search behavior using various options and flags.

In this tutorial, you will exclude in Grep. We will also address a few FAQs on how to exclude in Grep.

Exclude Words and Patterns

Use the -v (or --invert-match) option to display just the lines that do not match a search pattern.

To print the lines that do not contain the string nologin, run the following command:

grep -wv nologin /etc/passwd
Output

root:x:0:0:root:/root:/bin/bash
git:x:994:994:git daemon user:/:/usr/bin/git-shell
vega:x:1000:1000:vega:/home/vega:/bin/bash
👉
The -w option instructs grep to only return lines in which the provided string is a complete word (enclosed by non-word characters).

grep is case-sensitive by default. The uppercase and lowercase characters are treated separately in this scenario. Use the -i option with grep to ignore case while searching.

You must enclose the search string in single or double quotation marks if it contains spaces.

Use the -e option to define two or more search patterns:

grep -wv -e nologin -e bash /etc/passwd

The -e option can be used as many times as necessary.

Another technique for excluding several search patterns is to use the OR operator | to link the patterns together.

The lines that do not contain the strings nologin or bash are printed in the following example:

grep -wv 'nologin\|bash' /etc/passwd

Basic, Extended, and Perl-compatible regular expression syntaxes are supported by GNU grep. grep reads the pattern as a normal regular expression by default, which means that meta-characters like | lose their special significance and must be replaced with their backslashed variants.

The operator | should not be escaped if you use the extended regular expression option -E, as demonstrated below:

grep -Ewv 'nologin|bash' /etc/passwd

You can choose from a variety of possible matches, such as literal strings or expression sets. The lines where the string games appear at the very beginning of a line are eliminated in the following example:

grep -v "^games" file.txt

The output of a command can be filtered using grep and pipe, with just the lines matching a specific pattern being printed on the terminal.

For example, you can filter the output of the ps command to print out all running processes on your system except those running as user root:

ps -ef | grep -wv root

Exclude Directories and Files

You may want to exclude specific directories from the search result when using the -r or -R parameters to execute a recursive search.

🤔
The primary difference between the -r and -R options is that grep will follow all symbolic links when executed with capital R.

Use the --exclude-dir option to exclude a directory from the search. The excluded directory's path is relative to the search directory's path.

Here's how to look for the string vega in all files in the /etc directory, omitting the /etc/pki directory:

grep -R --exclude-dir=pki vega /etc

To exclude several directories, use curly brackets to enclose them and commas to divide them with no spaces.

For example, to discover files on your Linux system that include the string gnu, you would use the following command, excluding the proc, boot, and sys directories:

grep -r --exclude-dir={proc,boot,sys} gnu /

You can exclude files whose base name matches the GLOB supplied in the --exclude option when performing wildcard matching.

In the example below, we're looking for the string vega in all files in the current working directory, excluding files ending in .png and .jpg:

grep -rl --exclude=*.{png,jpg} vega *

FAQs to Exclude in Grep

How can I exclude a specific word or pattern in a grep search? 

Use the -v or --invert-match option with grep to exclude lines that match a specific word or pattern in the search.

Is it possible to exclude multiple patterns while using grep? 

Yes, you can exclude multiple patterns by combining them using logical operators such as -e, -f, or regular expressions with the grep command.

Can I exclude lines that match a whole word, not just a pattern, in grep? 

Yes, to exclude lines matching whole words, you can use the -w or --word-regexp option with grep.

How do I exclude specific files or directories from a recursive grep search? 

You can use the --exclude and --exclude-dir options to exclude specific files or directories respectively when performing recursive grep searches.

Can grep exclude matches based on case sensitivity? 

Yes, use the -i or --ignore-case option with grep to perform a case-insensitive search, excluding matches regardless of their case.

Is it possible to invert exclusion and only show matched lines using grep? 

Yes, you can invert the exclusion using the -v option with -v -v or --invert-match --invert-match to include only matched lines.

How can I exclude lines that match patterns from multiple files in a single grep command?

Use the -h or --no-filename option with grep to exclude filenames from the output when searching patterns across multiple files.

Conclusion

When searching for files, the grep tool allows you to exclude patterns and folders.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.