Tuesday, August 30, 2016

Grep for a particular file type

Grep is great for searching recursively through a directory of source code, but it annoyingly wastes time looking at binary files and other file types that I am not interested in. When I am looking through some Java code for a reference to something like writeTextToLog(), I would ordinarily go to my top level directory and use

grep -r "writeTextToLog(" .

But this returns lots of results like this that clutter the screen and sit there chewing up time:

Binary file ./cris-query-java/target/classes/com/mycompany/myproject/anotherdir/service/impl/MagicalLogWriter.class matches

Instead, I want to look just at .java files, so I can use the "--include" argument with grep to tell it what kinds of files to use. The value for this argument is a standard glob, so we can use the same kind of syntax we use with "ls" or "copy" from the Linux command line. My grep from above becomes:

grep -r --include="*.java" "writeTextToLog(" .

The above works with or without quotation marks around the glob. I can search my java files and my xml configuration files with a more complex glob (which, somewhat mysteriously, cannot be surrounded by quotes):

grep -r --include=*.{java,xml} "writeTextToLog(" .

And I get only the results I want and it happens much quicker. (I could also use the same syntax with "--exclude" if there is some particular file type I want to exclude instead of listing just the includes.)

For more examples of glob syntax, look here and here.

No comments:

Post a Comment