How to use logical OR in find?

In bash shell ls can use a logical OR functionality through (of course I could also do ls name1 name2 but my true examples are more complicated):

ls @(name1|name2)

Is there a way to do this using find?

My naive implementation:

find . -maxdepth 1 -name @("name1"|"name2") 

doesn’t work (it just outputs nothing)

Asked By: JeffDror


To answer your question, you can use -o option:

   expr1 -o expr2
          Or; expr2 is not evaluated if expr1 is true.

   expr1 -or expr2
          Same as expr1 -o expr2, but not POSIX compliant.

like this:

$ find . -maxdepth 1 -name "name1" -o -name "name2"
Answered By: Arkadiusz Drabczyk

You can use -o for logical OR. Beware however that all find predicates have logical values, so you’ll usually need to group ORed things together with parens. And since parens also have a meaning to the shell, you’ll also need to escape them:

find /some/dir -maxdepth 1 ( -name '*.c' -o -name '*.h' ) -print
Answered By: lcd047
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.