Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

I am using the following command for counting the lines of text in JAVA files:

find . -name '*.java' | xargs wc -l

How can I modify the find command parameters to match more than one file extension? For example, I would like use the above operation for CPP, C, and H files.

share|improve this question
This question is actually about the find command, since that is where you are searching for matching files. – iglvzx Apr 24 '12 at 23:48
Also, use either the find -print0 | xargs -0 construct or even better and simpler: find . -name '*.cpp' -o -name '*.c' -o -name '*.h' -exec wc -l {} +. This will avoid any file name issues (blank spaces, new lines and so on) and is (very) good custom. – Daniel Andersson Apr 25 '12 at 6:39
up vote 13 down vote accepted

Use the -o option for an OR. For example, this would list .cpp, .c and .h files:

find . -name \*.cpp -o -name \*.c -o -name \*.h
share|improve this answer
Ah. The * no longer needs to be escaped if formatted as code. Totally overlooked that. :) – iglvzx Apr 25 '12 at 0:44
Yep, it took three edits by two people but we got it. – JOTN Apr 25 '12 at 1:06
This didn't work for me on OSX (only matched the last -name *.ext) -- I had to use parentheses as suggested by @smokinguns below. – Gilead May 8 '15 at 11:37

You will need to use the -o option. For example the statement below finds all png, jpg and gif files in a folder.

find . \( -iname \*.png -o -iname \*.jpg -o -iname \*.gif \)

I use the -iname option so that the match is case insensitive.

share|improve this answer
iname isn't available on all versions of find. – JOTN Apr 25 '12 at 1:07
$ find /path/ -name '*.cpp' -or -name '*.c' -or -name '*.h'

The “-or” says I’m looking for either/both of two sets.

I recently wrote a quick guide to using find with boolean operators here:

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .