LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-22-2007, 01:03 AM   #1
dsuratman
Member
 
Registered: Nov 2003
Location: CA, USA
Distribution: Ubuntu 7.10
Posts: 32

Rep: Reputation: 15
Searching for a PATTERN in source files RECURSIVELY


I need help please. I'm relatively new to Linux, and I'd like to use grep to search for a pattern in files under several folders.

I'd like to find MyFunction in *.C files under MyRoot folder. The MyRoot folder itself doesn't contain any *.C file, however the sub-folders below it contain some *.C files.

This is what I did:
/Code/MyRoot$ grep -r MyFunction *.c

It complained that no *.c file or directory. However if I use:

/Code/MyRoot$ grep -r MyFunction .

It works, but it will find MyFunction in other type of files too (*.h, *.html) which sometimes I don't want.

Can someone tell me how to accomplish this please? Thanks.

Also, I'm used to working with C-code in MS-Windows environment using CodeWright. I'm trying to transition to Linux and would like to use gVim as my editor. Any clue on the best way to make this transition?

My primary goal is basically to be able to search some keywords or pattern within files spread out in multiple folders and jump to that file quickly and display it using gVim.

Thanks again.
 
Old 11-22-2007, 02:10 AM   #2
Pearlseattle
Member
 
Registered: Aug 2007
Location: Zurich, Switzerland
Distribution: Gentoo
Posts: 999

Rep: Reputation: 142Reputation: 142
Hi
About the recursive search: really weird that "grep -r" or "grep -R" does not work on your system.
Anyway alternatively you could use "find". E.g. ...
Code:
find . -name "*.c" -exec grep -l MyFunction '{}' \;
... will search all *.c files in all the subdirectories of the current directory and if any are found, it will execute on each of them the "grep" command. In AIX the "-l" flag of grep will display the filename instead of its contents, I don't have right now a Linux-system where to check which flag is used in there.
 
Old 11-22-2007, 02:44 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Pearlseattle View Post
Hi
About the recursive search: really weird that "grep -r" or "grep -R" does not work on your system.
It does work. The problem is that the *.c is evaluated
by the SHELL, not by grep. In other words: if there's NO
.c file in the current directory, it will complain as he
described it. Should there happen to be some, it will only
search those files.

Really the approach with find -exec grep is the most
feasible solution.




Cheers,
Tink

Last edited by Tinkster; 11-22-2007 at 02:46 AM.
 
Old 11-22-2007, 02:56 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Or maybe use egrep instead...
 
Old 11-22-2007, 11:03 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by syg00 View Post
Or maybe use egrep instead...
I'm curious.

How does that prevent the shell globbing *.c?




Cheers,
Tink
 
Old 11-22-2007, 01:15 PM   #6
dsuratman
Member
 
Registered: Nov 2003
Location: CA, USA
Distribution: Ubuntu 7.10
Posts: 32

Original Poster
Rep: Reputation: 15
The solution from PearlSeattle works. Thanks a lot.
 
Old 11-23-2007, 01:51 AM   #7
Pearlseattle
Member
 
Registered: Aug 2007
Location: Zurich, Switzerland
Distribution: Gentoo
Posts: 999

Rep: Reputation: 142Reputation: 142
You're welcome.
Could eventually work as well with

grep -r mytext "*.c"

This should prevent your shell from expanding the *.c into filenames if you already have in your source directory blabla.c-files.
Greetings
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively move files michaelsanford Linux - General 5 02-17-2016 01:24 AM
list recursively files with for xeon123 Programming 6 04-04-2007 03:38 PM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM
pattern searching - kindly help hinetvenkat Linux - Software 2 08-08-2005 11:19 PM
how to rm backup files recursively wazoo Linux - Newbie 6 12-31-2004 10:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:03 PM.

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