Use the -D
or -d
flag to describe the action of what to do with devices and directories respectively. There are three types of actions: read, skip and recurse. The skip action is the default.
Let’s take a look with directories since it will be easier to demonstrate.
Suppose you have a file readme.txt
in your current directory with the content:
Hello all!
Now you also have a subdirectory called alpha
containing a file named alpha.txt
with the following content:
// Located in ./alpha/alpha.txt
Hello alpha!
In order to search through all the files in the current directory for a matching search term we use an asterisk. For example:
$ grep "Hello" *
grep: alpha: Is a directory
readme.txt:Hello all!
This is essentially the same as using -d
with read as the action.
$ grep -d read "Hello" *
grep: alpha: Is a directory
readme.txt:Hello all!
Use the skip action to skip directories:
$ grep -d skip "Hello" *
readme.txt:Hello all!
Use the recurse action to recursively search through the directories:
$ grep -d recurse "Hello" *
alpha/alpha.txt:Hello alpha!
readme.txt:Hello all!
Want to see this in action? Check out the video below.