3

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.

1 Answers1

7

Just use 3 -names:

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

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/)

The / glob qualifier being the equivalent of find's -type d to select files of type directory.

With ksh93:

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

ksh93 has no glob qualifiers, but appending a / to the globs restricts the expansion to files that are directories or symlinks to directories.

With bash:

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

Same note as for ksh93 above about the trailing /.

Where:

  • Of course: find folder -name ... is recursive. It will find all directory entries (files and directories) inside any sub-directory. A maxdepth or prune is required to limit this –  May 11 '22 at 15:31