Exclude directories while using grep command – Tutorialspoint

Overview

We often run a grep command to search for specific strings of text within files. The grep command provides some additional functions that make searching even better. A feature that allows you to exclude certain directories from recurrence. This is useful when looking for large amounts of data.

Grep can be used with the −r option that will

allow you to specify multiple patterns and then use the −v option to display only those files that match your pattern

.

We will discuss the different ways to achieve this

.

Exclude single

directory

The easiest way to do this would be to simply add an exclusion directory name to the end of the file path. For example −

grep -r ‘pattern’ /path/to/directory1/*

This will find all files in the specified directory or any subdirectory. However, it will not exclude anything.

To exclude a single directory, you must include the −d flag. So, if you want to exclude the /home directory, you can use −

grep -rd ‘/home’ /path/to/*

Let’s create some files and folders to use as illustration.

$ mkdir tdir1 tdir2 tdir3 logs apache-logs $ echo “This is a sample text of tdir1/file1.txt file” > tdir1/file1.txt$ echo “This is a sample text of tdir2/file2.txt file” > tdir2/file2.txt $ echo “This is an example text of tdir3/file3.txt file” > tdir3/ file3.txt $ echo ” This is an example text of logs/service.log file” > logs/service.log$ echo “This is an example text of apache-logs/apache.log file” > apache-logs/apache.log

Let’s now look at the directory tree we just created − $ tree

-h . . ├── [4.0K] tdir1 │ └── [ 45] file1.txt ├── [4.0K] tdir1 │ └── 4.1 0K] tdir2 │ └── [ 45] file2.txt ├── [4.0K] tdir3 │ └── [ 45] file3.txt ├── [4.0K] records │ └── [ 47] service.log └── [4.0K] apache-logs └── [ 51] apache.log 5

directories, 5 files

We can use the −exclude−dir option of the grep command to exclude a directory − $ grep

-R “sample” -exclude-dir=tdir1 logs/service.log: This is an example text for logs/service.log file tdir3/file3.txt:This is an example text for tdir3/file3.txt file tdir2/file2.txt:This is an example text for tdir2/file2.txt apache-logs/apache file.log:This is an example text for the apache-logs/apache file.log

In the previous example, the grep command looks for a pattern in all directories except tdir1.

Exclude multiple directories

If you want to exclude more than one directory, you can combine them into a string using the pipe character (|). You can also use wildcards. For example, suppose you have two directories that you want to exclude: you

can use the characters * or ? to represent a single character. If you are looking for a literal asterisk (*), you should escape it by putting a backslash before it.

You can specify multiple directory options − exclude − to exclude multiple directories.

$ grep -R “sample” -exclude-dir=tdir1 -exclude-dir=tdir2 -exclude-dir=tdir3 logs/service.log:This is sample text from the logs/service file.log apache-logs/apache.log:This is sample text from the apache-logs/apache file.log In the

previous example, the grep command looks for a pattern in all directories except tdir1, tdir2, and tdir3

.

You can use an alternate syntax to achieve the same result. We can provide a list of directories in braces.

$ grep -R “sample” -exclude-dir={tdir1,tdir2,tdir3} logs/service.log:This is an example text of logs/service.log file apache-logs/apache.log:This is an example text of the apache-logs/apache file.log

Note that there should be no spaces before or after the comma

.

If

we

want to exclude many directories at once, we can often simply pair them using regular expressions. The grep command supports regular expression matching to exclude directories through *wildcard* characters.

? is used for zero or

  • an occurrence

  • of the previous character * is used for

  • zero or more occurrences of the previous character

  • \

  • is used to cite a wildcard

Let’s use the tdir pattern? to exclude the tdir1, tdir2, and tdir3 directories −

$ grep -R “sample” -exclude-dir=tdir? logs/service.log:This is an example text of logs/service.log file apache-logs/apache.log:This is an example text of the apache-logs/apache file.log Let’s use the logs\* and \*logs patterns

to exclude directories whose name begins or ends with

logs − $ grep -R “sample” -exclude-dir={logs\*,\*logs} tdir1/file1.txt:This is an example text of tdir1/file1.txt tdir3/file3.txt file: This is a sample text of tdir3/file3.txt tdir2/file2 file.txt:This is a sample text of the tdir2/file2 file.txt

Conclusion

We discuss three practical ways to exclude directories when repeating through the file system. These commands can be used in everyday life while using the Linux system.