find command: -or option doesn't work

I want to execute a command for an each file that matches a pattern. But the command that seems to be right, doesn’t work for me, I have no idea why.

$ find . -type f -name '*.c' -or -name '*.h' -or -name '*.cpp' -exec ls {} \;
$ ls
script.sh  test.c  test.h

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Try adding the expressions into parentheses as stated in the man page:

find . -type f \( -name '*.c' -or -name '*.h' -or -name '*.cpp' \) -exec ls {} \;

should work.

Solution 2

With GNU find, you can use -regex option:

find . -type f -regex '.*\.\(c\|h\|cpp\)' -exec ls {} \;

Solution 3

You can try to use find -D tree . [expr..] to understand what find does with your original command.

You must understand that the -type f and also the -exec ls .. expressions are and‘ed to the rest of the expressions with higher precedence than the ors.
So your original command will get parsed into something like this: (-type f AND -name *.c) OR -name *.h OR (-name *.cpp AND -exec ls) (note that the or is actually binary and not ternary so a | b | c is in fact (a | b) | c, but you get the point).
Now you will notice that find does not know what to do except for files matching *.cpp as there is no valid statement in the other cases (that’s why you may even see a segfault or something similar in the debug output).

I hope this makes it more clear to you why you need the parentheses.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply