Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
find . -name "*.[hc]|*.cc"

The above doesn't work,why?


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

share|improve this question
up vote 5 down vote accepted

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'

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.

share|improve this answer
P.S. I know you're not using a regex there, but you're mixing syntaxes: the | is regex-only, just like the * is shell-pattern-only – Michael Lowman Jul 14 '11 at 14:02
Is it possible to do with a single shell pattern? – kernel Jul 14 '11 at 14:14
And it seems wrong,the conditions are ANDed,but I want OR – kernel Jul 14 '11 at 14:16
@kernel the -o means or. This will match any file that ends in .c OR .h OR .cc. are you saying that this command doesn't produce the output you expect? – Michael Lowman Jul 14 '11 at 16:05
@kernel no. that was what i meant in the last paragraph – Michael Lowman Jul 14 '11 at 16:21

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.

share|improve this answer
much better regex than mine ;) also i really should have just looked up emacs – Michael Lowman Jul 14 '11 at 16:18
@MichaelLowman: I was just thinking the same about your's but I can't upvote it again. – Iain Jul 14 '11 at 16:20
Is it possible to do with a single shell pattern? – kernel Jul 14 '11 at 16:20
@kernel: moon on a stick too ? – Iain Jul 14 '11 at 16:26
What's the difference between -regextype posix-extended -regex and your -regex? – kernel Aug 4 '11 at 13:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.