3

I have an array of globs that I want to feed to find.

My array is:

arr=('a*.txt' 'b[2-5].sh' 'ab?.doc')

Here what i tried:

find . -type f -name '${arr[@]}'

Here our array may contain many elements! Thanks for your response!

1 Answer 1

5

The way to search for multiple glob patterns with find isn't as straightforward as find -name "${arr[@]}"; you need something equivalent to:

find '(' -name 'a*.txt' -o -name 'b[2-5].sh' -o -name 'ab?.doc' ')'

note: the parenthesis aren't mandatory in your case but you'll need them for adding other operands like -type f

That said, if your starting point is a bash array containing your globs, then you could build the arguments of find like this:

arr=('a*.txt' 'b[2-5].sh' 'ab?.doc')

names=()
for glob in "${arr[@]}"
do
    [[ $names ]] && names+=( -o )
    names+=(-name "$glob")
done

find '(' "${names[@]}" ')'
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. I'd recommend initializing names before the loop to make sure it starts empty, with names=().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.