3

Is there a way to prevent wildcard expansion on the find command so that the following would work?

find . -iname "foo [bar]"

As pointed out here, I tried running set -f, set -o noglob and setopt noglob just before find but it didn't work.

It's a bash script and I'm using Ubuntu 10.04.

I know I could just escape the wildcard characters but I fear I might leave one out.

1
  • 2
    What do you means by “would work”? The command you posted does work. If it doesn't do what you want, tell us what you want it to do. Commented Dec 8, 2013 at 22:47

2 Answers 2

4

As @njsg already said, the problem is caused by find, not the shell: The double quotes you are using are already protecting your expression from the shell; it is passed to find unmodified.

The problem is that find treats the argument to -name and -iname as a pattern. The only way to suppress that would be to use a find option that does not treat its argument as a pattern. But find has no such thing, according to my manual page, so what you want cannot be done. The best you can do is enhance your script to escape the brackets automatically, before passing them to find. The following funny-looking sed substitution will do that for you:

echo "foo [bar]" | sed 's/[][]/\\&/g'

Will print: foo \[bar\]

2
  • Thanks, alexis. I'll accept your answer for being the most complete and pushing me to a good path to do all the escaping I have to do.
    – lsborg
    Commented Dec 9, 2013 at 15:07
  • Good answer. Certainly GNU find has no such option (and I don't recall anybody ever asking for it). Commented Dec 9, 2013 at 22:55
4

The problem is not with the shell, but with find. The argument to -name and -iname is a pattern itself. [bar] will mean b, a or r to find. Try just escaping the square brackets, that is:

find . -iname 'foo \[bar\]'

(Edit: With this, you are not escaping them at the shell level, but passing the backslashes to find.)

2
  • Thanks, njsg. I'm looking for a solution that does not depends on escaping the wildcards.
    – lsborg
    Commented Dec 8, 2013 at 21:29
  • Ah, right, I missed that, sorry. As @alexis said, this largely depends on whether find supports it. And, from what @alexis said, it does not seem to. I'd indeed suggest alexis' sed idea.
    – njsg
    Commented Dec 8, 2013 at 23:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .