Quick review of basic find command before getting to the content.
$ find . -name "*.txt" -exec grep -l dog {} \;
Executing commands via exec must end with \; and often use {} as a placeholder for each file that the find commands locates. In the above example, imagine the command first locates the desired files, in this case files in the current and child directories that end with .txt. These X filenames are then inserted/replacing the curly brackets. Then, grep searches for 'dog' in each of those files and returns the filename of each file that matches.
Searching for a regex1 or regex2 in one pass can readily be accomplished by a single regex with one-pass file traversal.
$ find . -name "*.txt" -exec egrep -lo "reg1|reg2" {} \;
But, what if you're looking for matching files that include both regex1 and regex2?
Let's set up some simple files to play with, 3 files; cats.txt, dogs.txt, catsanddogs.txt
~/AdvancedFind$ more *.txt
::::::::::::::
catsanddogs.txt
::::::::::::::
cat
dog
::::::::::::::
cats.txt
::::::::::::::
cat
::::::::::::::
dogs.txt
::::::::::::::
dog
Finding files that contain 'cat' can be done by:
~/AdvancedFind$ find . -name "*.txt" -exec grep -li cat {} \;
./cats.txt
./catsanddogs.txt
Similarly, finding files that contain 'dog' can be done by:
~/AdvancedFind$ find . -name "*.txt" -exec grep -li dog {} \;
./catsanddogs.txt
./dogs.txt
You can 'chain' exec commands to satisfy finding files with 'cat' and 'dog' references.
~/AdvancedFind$ find . -name "*.txt" -exec grep -q cat {} \; -exec grep -l dog {} \;
./catsanddogs.txt
Files matching the first '-exec' command are passed to the second '-exec' command. Notice the '-q' suppresses the first grep output, otherwise you'll get the output from both exec commands.
Similarly, you can find files that contain 'dog' that don't contain 'cat' can be done by:
~/AdvancedFind$ find . -name "*.txt" -exec grep -q cat {} \; -exec grep -L dog {} \;
./cats.txt
The '-L' means 'files without match', resulting in the first exec command identifying files that contain 'cat', the second exec command matching files that don't have a 'dog' reference.
Chaining exec commands can help perform quick data analysis using commonly available utilities.
Cheers.