Skip to main content
4 of 4
added 69 characters in body
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*@(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#! /bin/bash -
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!

IFS='|' # "$*" joins with the first character of IFS
pattern="**/*@(${*})*"

IFS= # do globbing but not splitting upon unquoted expansion:
matches=( $pattern )

for element in "${matches[@]}"; do
  printf '%s\n' "${element}"
done

If you want to have it as a function, just put pattern=… to done in a function declaration.

Marcus Müller
  • 52.1k
  • 4
  • 80
  • 123