Easily Learn to Exclude Matches Directories Or Files with Grep

In this guide, we want to teach you to Exclude Matches Directories Or Files with Grep Command in Linux distros. As you must know, Grep is a useful Linux command that you can use in different ways to search input files for a search string and return the matching lines. One of its features is to exclude lines that match the given search string from the output while recurring through the directory tree. In simple words, you can find stuff with grep or you can choose to ignore them.

Steps To Exclude Matches Directories Or Files with Grep Command in Linux

To complete this guide, you must have access to your Linux server as a root or non-root user with sudo privileges. Then, follow the steps below to exclude directories, skip files, and select non-matching results while searching files with the Grep command in Linux.

Step 1 – Grep Command Syntax Options

The general syntax of Grep command is like:

grep <options> <search pattern> <files> <directory path>

The options that you can use for this guide include:

  • -w: match only whole words
  • -i: ignore case during search
  • -E: interpret patterns as extended regular expressions
  • -R: search recursively but follow all symlinks
  • -v: invert match
  • –exclude=: ignore files that match the pattern
  • –exclude-dir=: ignore directories that match the pattern

Now follow the steps below to Exclude Matches Directories Or Files with Grep.

Step 2 – Use Grep Command to Exclude Matches

To show the guide steps, we want to create an example directory and a text file:

# mkdir test_grep
# cd test_grep/
# vi test.txt

We add the following content to the file:

OrcaCore is a website which tries to provide useful IT Pro Tutorials in various fields of technology and different operating systems to its users.
For those who are interested in IT skills OrcaCore is here to response to your needs as a learning platform.

Now follow the steps below to see how to use the Grep command to exclude the matches.

First, we search for the Platform pattern in our test file with the command below:

grep platform test.txt

In the output, we get the line that includes the word platform.

Output
For those who are interested in IT skills OrcaCore is here to response to your needs as a learning platform.

Now you can use the -v option in the command to invert or exclude the match. It will show the non-matching results.

grep -V platform test.txt
Output
OrcaCore is a website which tries to provide useful IT Pro Tutorials in various fields of technology and different operating systems to its users.

As you can see, it shows the line that doesn’t have the word platform.

Also, if you want to return lines that do not include a specific pattern as a whole, which means a pattern surrounded by non-word characters, such as spaces, punctuation, or the start/end of a line, you can use the -w flag. For example:

grep -v -w IT test.txt

This will print nothing because the word IT is in the two lines and the Grep command excludes it.

To exclude multiple matches with Grep, you can use the -e option. For example:

grep -wv -e platform -e here test.txt

This will exclude the last line which contains these two words.

Instead of using the above syntax, you can use the -E option.

grep -v -E "fist_pattern|second_pattern" file

This will give you the same results.

Step 3 – Use Grep Command to Exclude Directories

At this point, you can use the -R option in Grep to recursively read and process all files under each directory and subdirectory. Here we have 3 directories and in each of them, there is a file with text. For example, we search for the following patterns in the working directory:

grep -E -R 'Olivia|Dani|Sandra'
Example Output
first-dir/lines1.txt:Olivia
second-dir/lines2.txt:Dani
third-dir/lines3.txt:Sandra

Now we want to exclude the first directory with the above query. To do this, use the –exclude-dir= option:

grep -E -R 'Olivia|Dani|Sandra' --exclude-dir=first-dir
Example Output
second-dir/lines2.txt:Dani
third-dir/lines3.txt:Sandra

As you can see, the results will not show the first directory.

Also, you can exclude multiple directories with the following grep command:

grep -E -R 'Olivia|Dani|Sandra' --exclude-dir={first-dir,second-dir}

Step 4 – Use Grep Command to Exclude Files

Excluding the files with grep is the same as the directories. You just need to specify the file name along with the –exclude-dir= option. For example, we have three files including Go, Cpp, and Py. We want to run a query with Hello that exists in three files and exclude the cpp file:

grep -R Hello --exclude=*.cpp
Example Output
first-dir/script.go:Hello World Go
third-dir/script.py:Hello World Python

The results will not show the cpp file.

Also, you can exclude the multiple files with the following command:

grep -R Hello --exclude=*.{cpp,py}

For more options and features, you can read the man page or help:

# grep --help
# man grep

Conclusion

At this point, you have learned to Exclude Matches Directories Or Files with Grep Command in Linux distros. Grep is a powerful and useful command that every Linux user can use in a variety of ways. Hope you enjoy it.

You may be interested in these articles:

Install Python 3.12 on Ubuntu and Debian Server

Install Config Server Firewall CSF on Debian 12

Check if PHP is Working on Apache in Ubuntu

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!