I often need to find a file, but I'm not sure what the name is, something like this:
$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'
This is a little tedious. I'd like to create an alias that's easier to use, like this:
$ findany foo bar blah
Here's my attempt:
findany() {
args=("-iname" "'*$1*'")
shift
while [ $# -gt 0 ]; do
args+=("-o" "-iname" "'*$1*'")
shift
done
find ${args[@]}
}
The problem is it never yields any results, even though the files are right there:
$ ls
bar.txt blah.txt foo.txt
$ findany foo bar blah
nothing
If I add echo
in front of the command, it looks correct:
$ findany foo bar blah
find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'
And if I copy the output above and run it, it works fine:
$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'
./bar.txt
./foo.txt
./blah.txt
I figure it has to do with argument splitting or quotes but I'm not sure how to debug it.
I normally use zsh but I verified the same behavior in bash.