LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 08: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, 08:22 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

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

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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:20 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
Open Source Consulting | Domain Registration