Linux – find files containing specific text

Handy command on Linux/Mac to find all files matching a pattern containing a specific text. This will also print file name.

$ find . -type f -name "*.php" -exec grep -H foo {} \;
./dir1/file1.php:function foo () {

Few points to note

  1. grep -H causes the command to print file name
  2. -type f will only run the command (specified by -exec) on files
  3. {} gets filled with file name.
  4. Backslash before semicolon (\;) is used to protect it from shell interpretation.
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments