Hello! First of all, I wrote this myself without AI (but with a stackover post here for the word counter (wc) part and here for the -exec of find part).
How it works
We select a directory called A. If A has a sub-directory at somepoint with the matching string "red" in it, we then ignore it. Otherwise, it is the deepest directory with "red" in its name. That makes sense. So, how do we do this?
First, I wrote a find.sh script
if [[ $(find "$1" -mindepth 1 -type d -name '*red*' | wc -c) -eq 0 ]]; then
echo "$1"
fi
It will output the name of the directory if it is the deepest match, or nothing if not. Then I do
find . -type d -name '*red*' -exec bash find.sh {} \;
Finally
find . -type d -name '*red*' -exec $SHELL -c 'if [[ $(find "$1" -mindepth 1 -type d -name "*red*" | wc -c) -eq 0 ]]; then echo $1; fi' sh {} \;
Hope this helps 👍