Linux : grep tips

This page last changed on Sep 03, 2008 by Kees de Kooter

Full text search all files in an entire directory and its subdirectories and display the file it was found in: (only java files in this example, containing the string "drop-and-create-tables")

 find -name "*.java" -exec grep -H "drop-and-create-tables" {} \; 

The result on my current box is:

./java/oracle/toplink/essentials/ejb/cmp3/EntityManagerFactoryProvider.java:    
public static final String DROP_AND_CREATE  = "drop-and-create-tables";

A different example:

find . -exec grep -q "some text" '{}' \; -print

Find all files containing <fmt:message/> tags and return the tag content:

find -name *.jsp -exec grep -o "fmt:message key=\"\([a-z\.]*\)\"" '{}' \;