Combining find, grep and possibly ls?!

Hi - can someone please help me combine find, grep and possibly ls into something workable:

i.e. How can I list all the files that contain the word "pet" in all directories under the current directory that are called "animal", bar those anywhere under directories called "archive"?

I suspect this will be a combination of grep -l pet , find . -type d -name animal
and prune , but can't quite work out what! :wall:

Any help most appreciated... :slight_smile:

run this under the directory animal

find -iname "*" -exec grep -li "pet" {} \; | grep -v archive

What shell and operating system are you using?

Try

find ./animal/* -name "*pet*" -ls | grep -v archive

EDIT: nm that, mis-read the question.:o

SunOS & Korn or Bash Shell

An example. The following files contain the word "pet":

archive_file.csv
file_2.csv
file_3.csv

Here they are in the directory structure:

/directory_1/
---animal/
------file_1.csv
------archive_file.csv
------directory_3/
---------file_4.csv
/directory_2/
---animal/
------file_2.csv
---archive/
------animal/
---------file_3.csv

I would only expect the command to return:

/directory_1/animal/archive_file.csv
/directory_2/animal/file_2.csv

...because "file_3.csv" is under an "archive" sub-directory.

(
 IFS='
'
 
 find $(
   find . -type d -name archive -prune -o -type d -name animal -print
     ) \
     -type f -exec grep -l pet {} +
   )

Works a treat! Thanks radoulov... :b: