Unix 'find' command to include/exclude subdirectories

Stan asked:

Say the folder structure looks like this:

.
|--folder1
   |--subfolder1
      |--subfolder2
   |--subfolder2
|--folder2
   |--subfolder1
      |--subfolder2
   |--subfolder2
|--folder3
   |--subfolder1
   |--subfolder2

I would like to find all files in subfolder2 only. I know I can just do this:

$ find . -type f |grep subfolder2

But was wondering if find comes with a option to include/exclude given directories?

My answer:


Just name all the directories you want to search.

find */subfolder2 -type f

Or for a completely arbitrary directory structure, something like this…

find $(find . -name subfolder2 -type d) -type f

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.