LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 12-14-2005, 09:15 PM   #1
wgillett
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:

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

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

Rep: Reputation: 118Reputation: 118
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, 12:55 PM   #4
wgillett
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!

-Walter
 
  


Reply


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-31-2004 12:37 AM
bash connot find the command Dunadan1821 Linux - Software 9 08-30-2004 09:04 AM
bash cant find format or mkfs command Dunadan1821 Linux - Software 3 08-12-2004 02:27 PM
Is this a valid command? find ./ -name bash.bashrc | cat davidas Linux - Newbie 11 04-06-2004 03:16 AM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 01:04 PM


All times are GMT -5. The time now is 03:49 AM.

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