Find Command – Find Files Containing Multiple Words in Any Order

find

I have a huge folder with a lot of subfolders where I would like to search for a folder that contains three words. Note that the folder name needs to have all three words, but the order of the words does not matter.

Example: I want to find folders containing the words APE, Banana and Tree.

find folder -name '*APE*Banana*Tree*'

However, this command will consider the order of the words, while this is not of interest and I want to find any folder with those words in any order.

Best Answer

Just use 3 -names:

find folder -name '*APE*' -name '*Banana*' -name '*Tree*'

Beware that like any time you use wildcards in the -name pattern that it may miss files whose name contains sequence of characters not forming valid characters in the user's locale.

With zsh:

set -o extendedglob # best in ~/.zshrc
print -rC1 -- **/(*Banana*~^*APE*~^*Tree*)(ND)

With ksh93:

FIGNORE='@(.|..)'
set -o globstar
set -- ~(N)**/@(*Banana*&*APE*&*Tree*)
(($# == 0)) || printf '%s\n' "$@"

With bash:

shopt -s globstar dotglob nullglob extglob
set -- **/!(!(*Banana*)|!(*APE*)|!(*Tree*))
(($# == 0)) || printf '%s\n' "$@"

Where: