Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 12-14-2005, 08:15 PM   #1
LQ Newbie
Registered: Oct 2005
Posts: 3

Rep: Reputation: 0
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:

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
Old 12-14-2005, 08:22 PM   #2
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Try this
find /some/directory -type f \( -name '*.h' -o -name '*.cpp' \) -exec ls {} \;
Old 12-14-2005, 09:46 PM   #3
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
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.
Old 12-15-2005, 11:55 AM   #4
LQ Newbie
Registered: Oct 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Re: using bash find command to list *.h and *.cpp

Thanks for the example, works great, thanks also for the info on shell options!



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble with find command in bash vrdhananjay Linux - Newbie 3 10-30-2004 11:37 PM
bash connot find the command Dunadan1821 Linux - Software 9 08-30-2004 08:04 AM
bash cant find format or mkfs command Dunadan1821 Linux - Software 3 08-12-2004 01:27 PM
Is this a valid command? find ./ -name bash.bashrc | cat davidas Linux - Newbie 11 04-06-2004 02:16 AM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 12:04 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration