Skip to content

How to exclude in Grep?

  • by
  • 3 min read

Linux has a bunch of command-line tools covering just about any action you’re going to need to perform on your PC. One such tool is grep — used to search one or more input files for lines that match a regular expression and write matching lines to standard output, much like find and replace.

But just like you can find things with grep, you can un-find things as well. In this article, we’re taking a look at how to exclude words, patterns or directories when using grep. 

Also read: Linux no space left on device: 3 Fixes


Excluding words and patterns with grep

Exclusion is called an inverted match in grep. To find words or patterns that do not match a specified search pattern, use the -v or —invert-match flag. 

For example, to search lines without the word “candid” in them, you’d use the following command.

grep -wv candid file1.txt
How to exclude in Grep?

In this example, the -w flag is used to tell grep to return only those lines where the specified search pattern (word) is a whole word. Another thing to keep in mind that is grep is case sensitive. You can use the -i flag to override this when searching. 

If the string you’re searching for includes spaces, you’re going to have to enclose it in single or double quotes. Additionally, you can specify two or more search patterns using the -e flag. Alternatively, you can use the OR operator (|) to separate search patterns. For example,

grep -wv -e candid -e technology file1.txt
How to exclude in Grep?

Or

grep -wv 'candid\|technology' file1.txt

Be sure to use either the backslashed version of the OR operator or include the -E flag, which enables extended regular expressions. 

Another possible combination here is the ps command. To print all running processes on your machine except the ones running as root, you’d use the following command. 

ps -ef | grep -wv root
How to exclude in Grep?

Excluding files and folders

Excluding folders in grep is relatively easy. Just include the -exclude-dir flag and mention the directory. If you want to add multiple directories, enclose them in curly brackets and separate them by commas. 

grep -R --exclude-dir=dir1 candid /dir0
How to exclude in Grep?
The output of the grep command. Matches are highlighted in red.

The above command will search all files inside dir0 for the word ‘candid’ except the files in dir0/dir1. Here’s how you’ll exclude multiple directories. 

grep -R --exclude-dir={dir1, dir2, dir3} /dir0

We’ll be using wildcard matching when working with files, which means you can use the –exclude flag.

grep -r --exclude=*.{png, jpg} candid *

The command mentioned above will search for the word ‘candid’ in all files except png and jpg in the current working directory. 

Also read: What is Kali Undercover and how to install it on Linux?

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>