Operations 6 min read

Mastering grep: Excluding Words, Patterns, and Directories Efficiently

This guide explains how to use grep's powerful options such as -v, -w, -e, -E, --exclude-dir, and --exclude to filter out specific words, patterns, and directories, providing clear command examples for precise text searching in Unix-like environments.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering grep: Excluding Words, Patterns, and Directories Efficiently

Excluding Words or Patterns

To display only lines that do not match a pattern, use the -v option. For example, to hide lines containing nologin:

[root@localhost ~]# grep -wv nologin /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bob:x:1000:1000::/home/bob:/bin/bash
user01:x:1001:1001::/home/user01:/bin/bash
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

The -w flag restricts matches to whole words, and -i makes the search case‑insensitive. When a search string contains spaces, enclose it in single or double quotes.

To exclude multiple patterns, repeat the -e option:

[root@localhost ~]# grep -wv -e nologin -e bash /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

Alternatively, with extended regular expressions you can combine patterns using the | operator and the -E flag, which removes the need to escape the pipe:

[root@localhost ~]# grep -Ewv 'nologin|bash' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

Excluding Directories or Files

To omit entire directories from a recursive grep, use --exclude-dir. The directory path is relative to the search root. Example: search for the word keys in /etc while skipping /etc/pki:

[root@localhost ~]# grep -Rw --exclude-dir=pki keys /etc/

Multiple directories can be excluded by listing them in braces, separated by commas (no spaces):

[root@localhost ~]# grep -r --exclude-dir={proc,boot,sys} gnu /

To exclude specific files, use --exclude. The pattern can contain wildcards. Example: search for linuxprobe in the current directory but ignore .png and .jpg files:

[root@localhost ~]# grep -rl --exclude=*.{png,jpg} linuxprobe *

Combining these options allows precise control over what is searched, making grep a versatile tool for system administrators and developers alike.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxGrepcommand-linetext searchdirectory exclusionpattern exclusion
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.