0

I am trying to find files with *.c and *.h extensions. I use the following command

find "$path" -type f -iname "*.c" -o -iname "*.h" -exec ls {} \;

This only executes ls the *.h matches. I have tried -o -iname *.c -o -iname *.h same thing happens. I would like to execute on both c and h files?

1 Answers1

4

You need to group the disjoint clauses:

find "$path" -type f \( -iname "*.c" -o -iname "*.h" \) -exec ls {} \;

Without that, find interprets your command as two disjoint sets of clauses: -type f -iname "*.c" on one hand, and -iname "*.h" -exec ls {} \; on the other. That’s why you only see *.h matches.

Incidentally, there’s not much point in running ls like that; you might as well use

find "$path" -type f \( -iname "*.c" -o -iname "*.h" \) -print

(but perhaps ls is a placeholder).

You can also simplify your command by combining the two globs:

find "$path" -type f -iname "*.[ch]"

(relying on the default -print action).

Stephen Kitt
  • 434,908