how to use the unix “find” command to find all the cpp and h files?

后端 未结 6 1945
南笙
南笙 2021-02-01 03:18

I know that to find all the .h files I need to use:

find . -name \"*.h\"

but how to find all the .h AND .cpp

相关标签:
6条回答
  • 2021-02-01 03:49
    find . -name \*.h -print -o -name \*.cpp -print
    

    or

    find . \( -name \*.h -o -name \*.cpp \) -print
    
    0 讨论(0)
  • 2021-02-01 03:51
    find -name "*.h" -or -name "*.cpp"
    

    (edited to protect the asterisks which were interpreted as formatting)

    0 讨论(0)
  • 2021-02-01 03:52

    Paul Tomblin Has Already provided a terrific answer, but I thought I saw a pattern in what you were doing.

    Chances are you'll be using find to generate a file list to process with grep one day, and for such task there exists a much more user friendly tool, Ack

    Works on any system that supports perl, and searching through all C++ related files in a directory recursively for a given string is as simple as

    ack "int\s+foo" --cpp 
    

    "--cpp" by default matches .cpp .cc .cxx .m .hpp .hh .h .hxx files

    (It also skips repository dirs by default so wont match on files that happen to look like files in them.)

    0 讨论(0)
  • 2021-02-01 03:54

    You can use find in this short form:

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

    -print can be omitted. Using -o just between expressions is especially useful when you want to find multiple types of files and do one same job (let's say calculating md5sum).

    0 讨论(0)
  • 2021-02-01 04:02
    find . -regex ".*\.[cChH]\(pp\)?" -print
    

    This tested fine for me in cygwin.

    0 讨论(0)
  • 2021-02-01 04:12

    A short, clear way to do it with find is:

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

    From the man page for -regex: "This is a match on the whole path, not a search." Hence the need to prefix with .* to match the beginning of the path ./dir1/dir2/... before the filename.

    0 讨论(0)
提交回复
热议问题