1. Overview

In Linux, we often use a grep command to search for text in files. The grep command provides a few additional functionalities that make the searching more effective. One such functionality is excluding directories while recurring through the directory hierarchy.

In this tutorial, we’ll discuss the various ways to achieve this.

2. Exclude Single Directory

Let’s create a set of files and directories to use as an example:

$ mkdir dir1 dir2 dir3 logs nginx-logs
$ echo "This is sample text from dir1/file1.txt file" > dir1/file1.txt
$ echo "This is sample text from dir2/file2.txt file" > dir2/file2.txt
$ echo "This is sample text from dir3/file3.txt file" > dir3/file3.txt
$ echo "This is sample text from logs/service.log file" > logs/service.log
$ echo "This is sample text from nginx-logs/nginx.log file" > nginx-logs/nginx.log

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

$ tree -h .
.
├── [4.0K]  dir1
│   └── [  45]  file1.txt
├── [4.0K]  dir2
│   └── [  45]  file2.txt
├── [4.0K]  dir3
│   └── [  45]  file3.txt
├── [4.0K]  logs
│   └── [  47]  service.log
└── [4.0K]  nginx-logs
    └── [  51]  nginx.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=dir1
logs/service.log:This is sample text from logs/service.log file
dir3/file3.txt:This is sample text from dir3/file3.txt file
dir2/file2.txt:This is sample text from dir2/file2.txt file
nginx-logs/nginx.log:This is sample text from nginx-logs/nginx.log file

In the above example, the grep command searches for a pattern in all directories except dir1.

3. Exclude Multiple Directories

We can use the –exclude-dir option multiple times to exclude multiple directories:

$ grep -R "sample" --exclude-dir=dir1 --exclude-dir=dir2 --exclude-dir=dir3
logs/service.log:This is sample text from logs/service.log file
nginx-logs/nginx.log:This is sample text from nginx-logs/nginx.log file

In the above example, the grep command searches for a pattern in all directories except dir1, dir2, and dir3.

There’s an alternate syntax to achieve the same results. We can provide a list of directories in curly braces:

$ grep -R "sample" --exclude-dir={dir1,dir2,dir3}
logs/service.log:This is sample text from logs/service.log file
nginx-logs/nginx.log:This is sample text from nginx-logs/nginx.log file

Note that there shouldn’t be any spaces before or after the comma.

4. Exclude Directories Using Pattern Matching

Sometimes it’s convenient to use pattern matching if we have a large number of directories to exclude. The grep command supports pattern matching to exclude directories via wildcards:

  • ? indicates zero or one occurrence of the previous character
  • * indicates zero or more occurrences of the previous character
  • \ is used to quote a wildcard

Let’s use the pattern dir? to exclude dir1, dir2, and dir3 directories:

$ grep -R "sample" --exclude-dir=dir?
logs/service.log:This is sample text from logs/service.log file
nginx-logs/nginx.log:This is sample text from nginx-logs/nginx.log file

Let’s use logs\* and \*logs patterns to exclude directories whose name either starts or ends with logs:

$ grep -R "sample" --exclude-dir={logs\*,\*logs}
dir1/file1.txt:This is sample text from dir1/file1.txt file
dir3/file3.txt:This is sample text from dir3/file3.txt file
dir2/file2.txt:This is sample text from dir2/file2.txt file

5. Conclusion

In this tutorial, we discussed three practical methods to exclude directories while recurring through the directory hierarchy. The commands shown in this tutorial can be used in day-to-day life while working with the Linux system.

Comments are closed on this article!