How to Exclude Patterns, Files, and Directories With Grep

This tutorial is about How to Exclude Patterns, Files, and Directories With Grep. We will try our best so that you understand this guide. I hope you like this blog, How to Exclude Patterns, Files, and Directories With Grep. If your answer is yes, please do share after reading this.

So lets keep reading for intertesting info:

Check How to Exclude Patterns, Files, and Directories With Grep

grep or global regular expression printing is a command line utility to search input files for a search string and return the matching lines; it searches the information that is piped to it or the files in the current directory. Surprisingly, this simple tool is by far one of the most useful Linux command line tools out there; provides several additional and useful features that make search queries less time consuming and more effective. We can achieve this using a wide range of command line options (flag keywords) and regular expression patterns.

The search process of this program is very efficient even when dealing with a large number of directories/files, grep does not store any lines; copies a line to a buffer, checks the search string, then prints the line if a match is found. Although the goal of this tool is to search for a given string, one of its features is to exclude lines matching the given search string from the output while iterating through the directory tree. In this article we will teach you how to exclude patterns, files, directories with grep.

How to exclude patterns, files and directories with Grep

Exclude words and patterns

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

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

  • grep -wv nologin /etc/passwd


  • root:x:0:0:root:/root:/bin/bashgit:x:994:994:git daemon user:/:/usr/bin/git-shellvega:x:1000:1000:vega:/home/ vega:/bin/bash

You must enclose the search string in single or double quotes 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 to exclude multiple search patterns is to use the OR | to match the patterns.

Perl-compatible, extended, and basic regular expression syntaxes are supported by GNU grep. grep reads the pattern as a regular regular expression by default, which means that metacharacters like | they lose their special meaning and must be replaced with their backslash variants.

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

  • grep -Ewv ‘nologin|bash’ /etc/passwd

You can choose from a variety of possible matches, such as literal strings or sets of expressions. Lines where string sets appear at the beginning of a line are removed in the following example:

  • grep -v “^games” file.txt

The output of a command can be filtered using grep and pipe, and only the lines that match a specific pattern are printed to the terminal.

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

Exclude directories and files

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

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

Here’s how to search for the string vega in all files in the /etc directory, skipping the /etc/pki directory:

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

To exclude multiple directories, use square brackets to enclose them and commas to divide them without 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 provided in the –exclude option when performing wildcard matching.

Final words: How to Exclude Patterns, Files, and Directories With Grep

I hope you understand this article, How to Exclude Patterns, Files, and Directories With Grep. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.

Editorial Staff
Editorial Staff
The Bollyinside editorial staff is made up of tech experts with more than 10 years of experience Led by Sumit Chauhan. We started in 2014 and now Bollyinside is a leading tech resource, offering everything from product reviews and tech guides to marketing tips. Think of us as your go-to tech encyclopedia!


Please enter your comment!
Please enter your name here

Related Articles

Best Telemedicine Software: for your healthcare practice

Telemedicine software has transformed my healthcare visits. It's fantastic for patients and doctors since they can obtain aid quickly. I...
Read more
I love microlearning Platforms in today's fast-paced world. Short, focused teachings that engage me are key. Microlearning platforms are great...
Think of a notebook on your computer or tablet that can be changed to fit whatever you want to write...
As of late, Homeschool Apps has gained a lot of popularity, which means that an increasing number of...
From what I've seen, HelpDesk software is essential for modern businesses to run easily. It's especially useful for improving customer...
For all of our important pictures, stories, and drawings, Google Drive is like a big toy box. But sometimes the...