2
find . -name "*.[hc]|*.cc"

The above doesn't work,why?

UPDATE

How do I find these 3 kinds of files with a single pattern?

kernel
  • 8,861

2 Answers2

5

It doesn't work because -name expects a shell pattern. You can use -regex instead, or just assemble your pattern like so:

find . -name '*.c' -o -name '*.h' -o -name '*.cc'

Edit
To do this with a single pattern you'll want a regex:

find . -regextype posix-extended -regex '.*\.(c|h|cc)'

You could do it with the default emacs regexes, I'm sure; I don't use them or know the main differences so I picked the one I know.

If you really want to use a single shellglob, you're out of luck: the only syntax for multiple strings is {a,b}, and this is not supported by find. But there's nothing wrong with the sort of command building/chaining in my first example. It's how find is intended to be used.

mdpc
  • 11,914
3

If you really want to use a regex then

 find -regex "\(.*\.[hc]\|.*\.cc\)"

should do the trick but the more normal way to do this is to use -o as already demonstrated.

user9517
  • 117,122