Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
1  
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

3 Answers 3

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
$ 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: http://jamesfishwick.com/2012/linux-find-and-boolean-operators

share|improve this answer

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
1  
iname isn't available on all versions of find. –  JOTN Apr 25 '12 at 1:07

Your Answer

 
discard

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.