Grep Exclude: Patterns, Files, and Directories

By 

Updated on

7 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 or ignore 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:

txt
grep [OPTIONS] PATTERN [FILE...]

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

Quick Reference

For a printable quick reference, see the grep cheatsheet .

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 (skips symlinks found while descending)
-RRecursive search (follows all symlinks)

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. Without -v, the same flag matches multiple patterns instead of excluding them.

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

The --exclude and --include options limit which files grep examines. They apply both to the files you name on the command line and to the files grep finds during a recursive search with -r or -R.

–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.

The two cases match the name in slightly different ways. During a recursive search, the glob is compared against the base name of each file. The base name is the part after the last slash. For files given on the command line, the glob is compared against a name suffix. A name suffix is either the whole name or a trailing part that begins right after a slash. In practice --exclude="*.log" behaves the same in both cases. A pattern containing a slash only ever matches command-line arguments.

–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 glob is matched against the base name of each subdirectory, not against a path relative to the search directory. The base name is the part after the last slash.

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

The match is on the base name, so this command also skips any other directory named pki further down the tree. A value containing a slash, such as --exclude-dir=etc/pki, does not match a subdirectory discovered while searching /etc. Command-line directory operands are different: GNU grep compares their name suffix, so a slash-containing glob can match a directory passed directly on the command line. To skip one specific path beneath a larger tree, search the parent directories you do want instead of excluding the one you do not.

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 how they treat symbolic links. Both follow the symlinks you name on the command line. During the descent into subdirectories, -r skips any symlink it meets, and -R follows all of them.

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

If you mostly need recursive project searches with file-type filters and ignore-file support, ripgrep can be simpler than combining several grep exclude options.

FAQ

What is the difference between -v and --exclude?
The -v option inverts line matching, so 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 make grep ignore a file or directory?
To ignore files by name during a recursive search, use --exclude="GLOB". To ignore directories, use --exclude-dir=DIR. Both options accept glob patterns and can be specified multiple times to skip several files or directories in one command.

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 .

Why does --exclude-dir with a path not work?
During recursive descent, --exclude-dir matches each subdirectory’s base name, not its path. A value such as build/cache does not match a discovered directory, while cache skips every directory with that name at any depth. GNU grep also compares command-line directory operands by name suffix, so a slash-containing glob can match a directory passed directly on the command line.

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, and both follow the symbolic links you name on the command line. The difference appears during the descent. -r skips symlinks it meets inside subdirectories, and -R follows them. 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.

Remember that -v works on lines, and the glob options work on file and directory names. A search that returns too much noise usually needs one of each. For the full set of options, see the grep command guide .

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