How to Exclude in Grep

By 

Updated on

5 min read

Grep Exclude

Grep is a command-line tool for searching files for lines that match a pattern. By default, it prints every matching line to standard output.

In many situations, you need the opposite — to exclude certain words, patterns, files, or directories from the search results. This guide explains how to use grep exclusion options with practical examples.

Grep Exclude Syntax

Use the following syntax when excluding matches, files, or directories:

Terminal
grep [OPTIONS] PATTERN [FILE...]

The most common options for exclusion are -v, --exclude, --include, and --exclude-dir.

Exclude Words and Patterns

To display only the lines that do not match a search pattern, use the -v (or --invert-match) option.

For example, to print the lines in /etc/passwd that do not contain the string nologin:

Terminal
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
linuxize:x:1000:1000:linuxize:/home/linuxize:/bin/bash

The -w option tells grep to match only whole words, so nologin does not match inside longer strings like nologinuser.

Exclude Multiple Patterns

To exclude more than one pattern, use the -e option for each pattern:

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

You can use -e as many times as needed.

Another option is to join the patterns with the OR operator (|). By default, grep interprets patterns as basic regular expressions, where | must be escaped:

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

With extended regular expressions (-E), the | operator does not need escaping:

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

Both commands produce the same result — lines that contain neither nologin nor bash.

Exclude Lines Matching a Position

You can use regular expressions to exclude lines where a pattern appears at a specific position. In the following example, lines where games appears at the beginning are excluded:

Terminal
grep -v "^games" file.txt

Exclude from Piped Output

A command’s output can be filtered through grep using a pipe. For example, to list all running processes except those owned by root:

Terminal
ps -ef | grep -wv root

Only the lines that do not contain the whole word root are printed.

Exclude Files

When performing a recursive search with -r or -R, you can limit which files grep examines using --exclude and --include.

–exclude

The --exclude option skips files whose names match a glob pattern. In the following example, grep searches for linuxize in all files in the current directory but skips .png and .jpg files:

Terminal
grep -rl --exclude="*.png" --exclude="*.jpg" linuxize .

You can use --exclude multiple times to skip different file types.

–include

The --include option is the opposite of --exclude — grep searches only the files whose names match the specified pattern. For example, to search only .conf files:

Terminal
grep -rl --include="*.conf" linuxize /etc

This is useful when you want to narrow the search to a specific file type rather than listing everything to exclude.

Combining –include and –exclude

You can use both options together. The following command searches only .log files but skips any file named debug.log:

Terminal
grep -rl --include="*.log" --exclude="debug.log" error /var/log

Exclude Directories

When searching recursively, use --exclude-dir to skip specific directories. The path is relative to the search directory.

The following example searches for linuxize in all files under /etc but skips the /etc/pki directory:

Terminal
grep -R --exclude-dir=pki linuxize /etc

To exclude multiple directories, use --exclude-dir for each one:

Terminal
grep -r --exclude-dir=proc --exclude-dir=boot --exclude-dir=sys gnu /

The difference between -r and -R is that -R follows symbolic links while -r does not.

Combining File and Directory Exclusions

You can combine --exclude, --include, and --exclude-dir in a single command. For example, to search .conf files under /etc while skipping the pki and ssl directories:

Terminal
grep -rl --include="*.conf" --exclude-dir=pki --exclude-dir=ssl error /etc

Quick Reference

OptionDescription
-vInvert match — show lines that do not match
-e patternSpecify a pattern (use multiple times to exclude several)
--exclude="GLOB"Skip files matching the glob pattern
--include="GLOB"Search only files matching the glob pattern
--exclude-dir=DIRSkip directories matching the name
-rRecursive search (does not follow symlinks)
-RRecursive search (follows symlinks)

FAQ

What is the difference between -v and --exclude?
The -v option inverts line matching — it shows lines that do not contain the pattern. The --exclude option skips entire files based on their filename. They operate at different levels.

How do I exclude multiple directories?
Use --exclude-dir once for each directory you want to skip: grep -r --exclude-dir=dir1 --exclude-dir=dir2 pattern .

Can I use regular expressions with --exclude or --exclude-dir?
No. These options accept only glob patterns (like *.log or test_*), not regular expressions.

What is the difference between -r and -R?
Both search directories recursively. The difference is that -R follows symbolic links while -r does not. Use -r to avoid searching the same files twice through symlinks.

How do I exclude case-insensitively?
Add the -i option to make the pattern match case-insensitive: grep -vi pattern file.txt. The --exclude and --exclude-dir options always use case-sensitive glob matching.

Conclusion

Grep provides several options for excluding content from search results. Use -v to invert line matching, --exclude and --include to filter files by name, and --exclude-dir to skip directories during recursive searches.

If you have any questions, feel free to leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page