Searching for a PATTERN in source files RECURSIVELY
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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. ...
... 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.