How to Exclude with Grep in Linux

Wondering how to exclude with Grep in Linux?

Global regular expression print, better known as Grep, is a Command-line tool that outdates Linux itself. However, its usability has made it stand the test of time.

Developed by Ken Thompson, Grep is a search-based utility that is used to find lines of text that are specified by the user.

Grep allows you to exclude certain keywords, lines, and even directories during a scan. This allows for the efficient retrieval of data through text files.

This guide will educate you all about the Grep command, its requirements, flags, and more.

So, let’s begin!

Grep Requirements.

Before using the Grep command, it’s important to see whether you have the GNU version of the command available.

Normally, the command is available for any Linux distribution. However, you can check for its availability by typing the following command in the Terminal:

$ grep --version

This will display the version of Grep that’s available on your Linux distribution.

If you have the GNU version of Grep available, you can proceed to the next step. If not, checking out this resource might be helpful.

For this guide, we’ll be using Ubuntu 20.04 LTS. However, the method shouldn’t be any different for other Linux distributions.

Using The Grep Command.

Now that we’ve covered the prerequisites for the Grep command, it’s time to learn how to use it.

The syntax for the Grep command is as follows:

$ grep <options> <string> <directory>
Exclude with Grep in Linux

Here is a list of options that are available with the Grep command:

OptionUse
-wSignals the command to return lines with strings that are complete words
-vOutputs lines that do not match the searched strings
-eDeclares more than one search keyword/pattern
-r or -RUsed for recursive search
–exclude-dirUsed to exclude directories from search
-lUsed to specify case sensitivity

Although many options are available with the Grep command, this guide will only cover the methods of excluding text. 

By the end of this guide, you’ll have a decent idea of how to exclude in Grep in Linux.

Excluding Words Using Grep.

One of the key features of grep is its ability to perform an efficient search by excluding certain keywords, strings, or directories.

To show how excluding works, we’ll create a text file called test.txt. You can access the content of the file using the $ cat command.

$ cat test.txt
Exclude with Grep in Linux

The contents of the text file are shown in the image below:

Follow these steps to exclude words using Grep:

  1. Start by opening the Command Terminal. You can achieve this by pressing Ctrl + Alt + T on the keyboard.
  2. Next, type in the following command:
$ grep -v -I -E ‘<keyword>’  test.txt

In our case:

$ grep -v -I -E ‘Windows’ test.txt
Exclude with Grep in Linux

This will display all text in the file except for strings that contain the word “Windows” in them.

Excluding Multiple Words.

Grep can also be used to exclude multiple words in a single command.

To achieve this, the -e flag is used.

Follow these steps to exclude multiple words using Grep:

  1. First, open the Command Terminal.
  2. Next, type the following:
$ grep -v -e “<word 1>” -e “<word 2>” <filename.txt>

In our case:

$ grep -v -e “mac OS” -e “Windows” test.txt
Exclude with Grep in Linux

This will remove all strings that have the words “mac OS” and “windows”

Alternatively, you can also type the following in the Command Terminal:

$ cat test.txt | grep -v -e “mac OS” -e “Windows”
alternatie command

To make the text case-sensitive, include the -I flag.

Excluding Complete Files.

Grep can also be used to ignore a complete file.

For this purpose, type the command as follow:

$ grep - exclude “<filename>”.txt grep *.txt

This will proceed to remove the complete file from your system.

Excluding Complete Directories.

You can also use the Grep command to exclude complete directories.

You can achieve this by specifying certain words. These words, if found in a directory, will cause the entire directory to be excluded.

To exclude directories using words, follow these steps:

  1. Start by opening the Command Terminal. You can achieve this by pressing Ctrl + Alt + T on the keyboard.
  2. Next, type the following:
$ grep - -exclude-dir “mac OS” –R “grep”

This will exclude the directory containing the file with the aforementioned string.

It should be noted that there’s no need to specify the name of the file in this command.

The command makes use of the -R flag, which is used for recursive search and is necessary when searching through directories.

With the steps followed correctly, you should have successfully ignored the directories with the specified words.

Excluding Words By Specifying Directories.

The Grep command can also be used to exclude words by specifying the directory.

Follow these steps to exclude words by specifying directories:

  1. First, open the Command Terminal.
  2. Next, type the following to exclude text using directories:
$ grep –R “<word>” <directory> | grep –v “exclude this”

In this case:

$ grep -R “mac OS” /home/Ubuntu | grep -v “exclude this”

The system will output the text after performing a recursive search.

recursive search

While on the topic of text files, it is necessary to have tools that can manipulate the contents. You might find yourself in situations where you’d need to add and remove multiple lines in blocks of text. Having a decent text editor can really speed up your work when it comes to editing the files.

One of the best text editors that are available on Linux is the gedit text editor. Using the text editor in dark mode, however, can make you face some difficulty in reading the highlighted text. Although the issue hasn’t been fixed by the developers, there are ways to have a temporary fix.

test output

We hope this guide helped you learn how to exclude in Grep in Linux. If you have any questions or queries, or suggestions to make this guide better, let us know in the comment section.

Leave a Reply

Your email address will not be published.

Previous Post

How to Use the Basename Command in Linux

Next Post

How to Use the Csplit Command in Linux

Related Posts