0

I have a directory tree as shown below. In words, the root directory contains multiple sub-directories, with arbitrary names. each of which may contain two folders: "OK" and "NotOK". These folders contain images classified as "happy" or "sad", which is reflected by their names.

root/
    dir0/
        OK/
            img_happy_001.png
            img_happy_002.png
            img_sad_001.png
            ...
        NotOK/
            img_happy_103.png
            img_happy_104.png
            img_sad_72.png
            ...
    dir1/
        OK/
            img_happy_501.png
            img_sad_233.png
            ...
        NotOK/
            img_happy_703.png
            img_happy_704.png
            img_sad_298.png
            ...
    ...

I could easily find all images classified as "happy" by doing

find . -name "*happy*.png"

However, I would like to find all images classified as "happy" that are in an "OK"-directory. How can I do this?

filiphl
  • 921
  • 1
  • 8
  • 15

2 Answers2

2

you could simply pipe the results from find into grep:

find . -name "*happy*.png" | grep -v NotOK

The grep command will filter the results from find that do not contain NotOK.

Alternatively you can look at --whole-name option in find.

I believe you would need something like:

find . -name "*happy*.png" -wholename "\*/OK\*"

user7440787
  • 831
  • 7
  • 22
0

I acquired the desired result using

find . -type d -name "OK" -exec find {} -name "*happy*" \; 
filiphl
  • 921
  • 1
  • 8
  • 15