Recursive grep with only certain types of files

Can I please have some ideas on how to do a recursive grep with certain types of files? The file types I want to use are *.c and *.java.

I know this normally works with all files.

grep -riI 'scanner' /home/bob/ 2>/dev/null

Just not sure how to get it to work *.c and *.java files.

Like this ???

$ grep --include="*.c" -RIHn "scanner"

Do you think there is an error? I tried that and didn't get any output. I let the program run for about an hour until I finally killed it.

Sorry its my fault missed dot (.) :slight_smile:

Try :

$ grep --include="*.java" -RIHn "pattern" .

Example :

$ grep --include="*.java" -RIHn "public static void main" .
./9.java:3:    public static void main(String[] args) {
./test1.java:3:    public static void main(String[] args) {
./8.java:3:    public static void main(String[] args) {
./7.java:3:    public static void main(String[] args) {
./10.java:3:    public static void main(String[] args) {
./3.java:3:    public static void main(String[] args) {
./2.java:3:    public static void main(String[] args) {
./5.java:3:    public static void main(String[] args) {
./4.java:3:    public static void main(String[] args) {
./1.java:3:    public static void main(String[] args) {
./6.java:3:    public static void main(String[] args) {
./test.java:3:    public static void main(String[] args) {

Works perfectly :). Thank you. Any idea why you need to include the "."? Usually grep is smart enough to start in your current directory.

As far as I know include is just for pattern "." is to start in current directory as you said. similar to find . -type f | xargs .......