I'd like to find source files (*.c, *.cpp, *.h) that contain in Linux/MinGW/Cygwin, and recursively in all sub directories.

My basic idea is using find and grep. However, building a regular expression that can check given file name is either *.c, *.cpp, or *.h isn't easy. Could you help me out?

This should work:

find Linux/MinGW/Cygwin -name '*.c' -o -name '*.cpp' -o -name '*.h'
find -regex '.*/.*\.\(c\|cpp\|h\)$'

I would use:

find . -regex '.*\.\(c\|cpp\|h\)$' -print
  • 1
    In my macbook, the script without a -E can't find anything. you might need add a -E (as extended) to make it more portable, just like find -E . -regex '.*\.(c|h|cpp)' -print. ;-) – YanTing_ThePandaMay 1 '17 at 9:17

Quick and dirty, and avoids directory names:

find . -type f -name *.[c\|h]
  • 2
    There are three problems with this command: 1. It won't find *.cpp files. 2. It will find *.| files. 3. The glob will expand if there are matching files in the current directory. Quoting prevents that. – Dennis Dec 17 '13 at 15:48

I use a mac pro which also works in bash. But every time I type in the command line:

find -name

it says illegal option. So I just simplified it as:

find *.c *.cpp *.h

and found it really worked!

You can use a slightly simpler regex:

find . -type f -regex ".*\.[ch]\(pp\)?$"

블로그 이미지



댓글을 달아 주세요