LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   using bash find command to list *.h and *.cpp (https://www.linuxquestions.org/questions/linux-software-2/using-bash-find-command-to-list-%2A-h-and-%2A-cpp-392930/)

wgillett 12-14-2005 08:15 PM

using bash find command to list *.h and *.cpp
 
I can do this to get all the .h files in the current dir and subdirs:

find . -name *.h

and can get all the .cpp files as a minor variation. But how do I get .h *and* .cpp files with a single "find" call? First I figured out that I need to do this:

shopt -s extglob

to turn on fancier pattern matching. That allows operators like this:

+(pattern-list)
Matches one or more occurrences of the given patterns

I am able to use this technique with "ls":

ls *+(h|cpp)

to get *.h and *.cpp.

But how to do this with "find"? This:

find . -name '*+(cpp|h)'

doesn't work.

-Walter Gillett

homey 12-14-2005 08:22 PM

Try this
Code:

find /some/directory -type f \( -name '*.h' -o -name '*.cpp' \) -exec ls {} \;

Matir 12-14-2005 09:46 PM

As a further note, find is NOT a bash builtin, and will so be unable to make use of those extended shell options. /bin/ls would be equally unable to do so, but the bash builtin ls can use it. :) Tiny little tricks that will catch you.

wgillett 12-15-2005 11:55 AM

Re: using bash find command to list *.h and *.cpp
 
Thanks for the example, works great, thanks also for the info on shell options!

-Walter


All times are GMT -5. The time now is 04:21 AM.