Why does `find` in Linux skip expected results when `-o` is used?

mastan

Why in Linux (Debian 8)

touch 1.cpp 1.h
find . -name "*.cpp" -o -name "*.h" -exec echo {} \;

outputs only 1.h while

find . -name "*.cpp" -o -name "*.h"

outputs both? Is it a bug or feature?

kenorb

I think once you used -or operator, then you've to keep it consistent in order to avoid ambiguous order of logical operations when you have multiple conditions connected using logical OR.

It seems the -exec part is grouped together with the second -name "*.h".

So in order to make it work correctly, you've to add the brackets as below:

find . '(' -name '*.cpp' -o -name '*.h' ')' -exec echo {} ';'

Remember: The parentheses must be quoted or escaped with a backslash to prevent them from being interpreted as special shell characters.

Alternatively combine few extensions into one by using -regex:

find . ! -regex ".*\.\(cpp\|h\)" -exec echo {} \;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does not this DL query return expected results?

From Dev

Why does this line of GLSL for calculating distance produce inconsistent results, when this one works as expected?

From Dev

Why does apply skip first element of array used for arguments?

From Dev

Lucene.Net not returning expected search results when "-" or wildcards are used

From Dev

In "find ... | xargs ...", why does xargs iterate even when find returns zero results?

From Dev

Why does my GNU make skip making an object file (%.o) when building an %.s (assembler) program?

From Dev

Why does Find in Excel give unexpected results?

From Dev

Why does the regex in find not return any results?

From Dev

C++ Why does the iterators behave differently when used with the find function?

From Java

Why does find return different, sorted results when passed the current directory versus a path?

From Dev

Why does Linux become unresponsive when a large number of memory is used (OOM cannot be triggered)

From Dev

What does "-ls" do when used with "find"?

From Dev

WHy does this code crash, when used with bin?

From Dev

Why does the results of the bootstrapping methods differs if it is being used the same seed?

From Dev

Why does xargs skip first argument when passing to subshell?

From Dev

Why does When.js promise.then skip a function?

From Dev

Why does the program skip taking input when function is called?

From Dev

Skip'ing on a Source does not work as expected

From Dev

Skip'ing on a Source does not work as expected

From Dev

Why do some Linux utilities output Unicode (when it is not expected)?

From Dev

Why does NVL2 returns NULL when it's not expected?

From Dev

Why does NVL2 returns NULL when it's not expected?

From Dev

Why does NSNumberFormatter skip a number?

From Dev

Why does it skip the "control" segment?

From Dev

Why is find piping in non directories when the “-type d” test is used?

From Dev

Why is addBack used with Find()

From Dev

Alternative rules in PARSE with SKIP only did not output expected results

From Dev

Alternative rules in PARSE with SKIP only did not output expected results

From Dev

Why does eps fail in a matrix when used with realmax

Related Related

  1. 1

    Why does not this DL query return expected results?

  2. 2

    Why does this line of GLSL for calculating distance produce inconsistent results, when this one works as expected?

  3. 3

    Why does apply skip first element of array used for arguments?

  4. 4

    Lucene.Net not returning expected search results when "-" or wildcards are used

  5. 5

    In "find ... | xargs ...", why does xargs iterate even when find returns zero results?

  6. 6

    Why does my GNU make skip making an object file (%.o) when building an %.s (assembler) program?

  7. 7

    Why does Find in Excel give unexpected results?

  8. 8

    Why does the regex in find not return any results?

  9. 9

    C++ Why does the iterators behave differently when used with the find function?

  10. 10

    Why does find return different, sorted results when passed the current directory versus a path?

  11. 11

    Why does Linux become unresponsive when a large number of memory is used (OOM cannot be triggered)

  12. 12

    What does "-ls" do when used with "find"?

  13. 13

    WHy does this code crash, when used with bin?

  14. 14

    Why does the results of the bootstrapping methods differs if it is being used the same seed?

  15. 15

    Why does xargs skip first argument when passing to subshell?

  16. 16

    Why does When.js promise.then skip a function?

  17. 17

    Why does the program skip taking input when function is called?

  18. 18

    Skip'ing on a Source does not work as expected

  19. 19

    Skip'ing on a Source does not work as expected

  20. 20

    Why do some Linux utilities output Unicode (when it is not expected)?

  21. 21

    Why does NVL2 returns NULL when it's not expected?

  22. 22

    Why does NVL2 returns NULL when it's not expected?

  23. 23

    Why does NSNumberFormatter skip a number?

  24. 24

    Why does it skip the "control" segment?

  25. 25

    Why is find piping in non directories when the “-type d” test is used?

  26. 26

    Why is addBack used with Find()

  27. 27

    Alternative rules in PARSE with SKIP only did not output expected results

  28. 28

    Alternative rules in PARSE with SKIP only did not output expected results

  29. 29

    Why does eps fail in a matrix when used with realmax

HotTag

Archive