Search for files looking for a particular pattern

To search for all files of a certain type containing a line of a certain pattern, use find with the -exec option.

find . -name "<filepattern>" -exec grep -n "<linepattern>" /dev/null {} \;

For example, the following searches for all files of the form *.pm (files with names ending in ".pm") for the line "stamp_file".

find . -name "*.pm" -exec grep -n "stamp_file" /dev/null {} \;

Here the /dev/null is a trick to make grep print the file name, and the -n makes grep print the line number. The {} and the final \; tell find where to put the file name and where the command ends.

Web links


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer