0

Consider the following:

/tmp/tmp.DkL0R4v7RR$ find . -regex '\./spam' -o -regex '\./eggs'
./spam
./eggs
/tmp/tmp.DkL0R4v7RR$ find . -regex '\./spam' -o -regex '\./eggs' -exec ls \{} \;
./eggs
/tmp/tmp.DkL0R4v7RR$ find . -regex '\./spam' -o -regex '\./eggs' | xargs ls
./eggs  ./spam

Why does the appended -exec alter the behaviour so that only the second regex is matched?

karlsebal
  • 815
  • yes, partly. I always guessed this and tried find . "(-regex '\./spam' -o -regex '\./eggs')" -exec ls \{} \; but to no avail. It’s not entirely clear to me why it has to be escaped like that, but … well, thanks, anyway – karlsebal Apr 06 '22 at 10:37
  • The ( and ) need to be escaped because they're part of the shell syntax (for starting subshells and other uses), {} doesn't need to be escaped in some shells, though. – muru Apr 06 '22 at 10:48
  • Yes, I am aware of that. But I do not understand why "( … )" does not work resp. why '(' and \) need to be single arguments on the command line. – karlsebal Apr 06 '22 at 10:52
  • 2
    find is not like sed (or awk) where you can provide code as a string and the tool parses it. find expects operands as separate arguments. It performs neither word splitting nor quote removal; these are jobs of the shell. – Kamil Maciorowski Apr 06 '22 at 11:54

0 Answers0