How to use unix unix command to find all cpp and h files?

I know that to search for all .h files that I need to use:

 find . -name "*.h" 

but how to find all .h AND .cpp files?

+26
unix find
Nov 10 '08 at 14:35
source share
6 answers
 find . -name \*.h -print -o -name \*.cpp -print 

or

 find . \( -name \*.h -o -name \*.cpp \) -print 
+39
Nov 10 '08 at 14:35
source share
 find -name "*.h" -or -name "*.cpp" 

(edited to protect stars that were interpreted as formatting)

+12
Nov 10 '08 at 14:40
source share

Paul Tomblin Already gave a terrific answer, but I thought I saw a pattern in what you did.

Most likely, you will use find to create a list of files to be processed with grep one day, and for such a task there is a much more convenient tool, Ack

It works on any system that supports perl, and searching all C ++ related files in a directory recursive for a given string is as simple as

 ack "int\s+foo" --cpp 

"--cpp" defaults to .cpp .cc .cxx .m .hpp .hh .h .hxx files

(It also skips repository repositories by default, so it is not consistent with files that look like files in them.)

+6
Nov 10 '08 at 15:39
source share

A short, clear way to do this with find :

 find . -regex '.*\.\(cpp\|h\)' 

On the man page for -regex : "This is a match all the way, not search." Therefore, you need the prefix with .* To match the beginning of the path ./dir1/dir2/... before the file name.

+5
Apr 30 '15 at 8:59
source share
 find . -regex ".*\.[cChH]\(pp\)?" -print 

This is tested perfectly for me at cygwin.

+2
Oct 04 '10 at
source share

You can use find in this short form:

 find \( -name '*.cpp' -o -name '*.h' \) -print 

-print can be omitted. Using -o only between expressions is especially useful when you want to find several types of files and do the same job (say md5sum calculation).

+2
Aug 07 '14 at 5:19
source share



All Articles