how to make grep to search a pattern in only specific file type
Hello there;
To search a string pattern in all files in a directory and subdirectories, I am using; Code:
grep -R "myclass::my-func(" mydirectory/ Best Regards. |
find mydirectory/ -name "*.cc" -type f -exec grep -l "myclass::my-func(" {} \;
|
Thanks troop,
The result of your command is exactly what I wanted. But I will take time to understand the command.. |
Troop is right, but this enhancement is quite useful:
Code:
find mydirectory/ -name "*.cc" -type f -exec grep -lH "myclass::my-func(" {} \; jlinkels |
Thanks jlinkels, but I see no difference;
Code:
[mq15@localhost ns234]$ find ns-allinone-2.34/ -name "*.cc" -type f -exec grep -lH "::purge(" {} \; |
Quote:
I think in this case(when -l presented), this option is useless. Probably he meant: Code:
find ns-allinone-2.34/ -name "*.cc" -type f -exec grep -H "::purge(" {} \; or Code:
find ns-allinone-2.34/ -name "*.cc" -type f -exec grep -n "::purge(" {} \; |
Yes, -l already lists matching files, and -H lists the file name in addition to the matching string.
My preference is for -H as often I make mistakes in the regular expression so I wnat to see the matching string and I want to see in which file it is found. -l als suffices in many cases of course. jlinkels |
Thanks to both of guys. Really useful information.
Have a nice day |
All times are GMT -5. The time now is 10:50 AM. |