I'm finding that when I use find's -or operator in combination with -exec, I don't get the result I expect. When searching for .cpp and .h files, the command works as expected if I don't use -exec:
find . -name '*.cpp' -or -name '*.h'
./file1.cpp
./file1.h
./file2.cpp
./file2.h
However, when I use -exec, only the .h files seem to be passed:
find . -name '*.cpp' -or -name '*.h' -exec echo '{}' \;
./file1.h
./file2.h
It works fine when I use the more generalized approach for returning the result:
echo $(find . -name '*.cpp' -or -name '*.h')
./file1.cpp ./file1.h ./file2.cpp ./file2.h
However, I'd like to know what I'm doing wrong with -exec, as it's often more convenient. I'm using Mac OSX 10.9, but the same issue occurs in a Cygwin terminal. What's going wrong here? How can I make -exec work the way I want?
– wonderlr Sep 28 '14 at 18:25find . -name '*.cpp' -exec echo '{}' \; -or -name '*.h' -exec echo '{}' \;
-exe action is tied to the -name "*.h"
. Namely that-or
has a lower order of precedence in find than-a
, which is implied in between every juxtaposed set of expressions that isn't specifically separated with another operator. Also,xargs
is faster than-exec echo ... \;
not-exec echo ... +
– Oct 05 '14 at 07:32