Searching file contents using find for multiple file extensions in Linux

| | 1 min read

If you use GNU/Linux as your development environment then you inevitably has to use the find command at some point of time. A very common need while developing web applications is to search for the occurrences of a given string in files, very often with different extensions, under a given project. The command 'find' is a very powerful search utility that can be wielded according to our convenience and can be used in this scenario.

We work a lot in Drupal where we have this specific requirement where we need to search in files with different file extensions. We use a simple script for this. Copy the following into a file and save it as /usr/local/bin/search


find $1 \( -name "*.php" -o -name "*.inc" -o -name "*.phtml" -o -name "*.php3" -o -name "*.xml" -o -name "*.htm" -o -name "*.html" -o -name "*.engine" -o -name "*.theme" -o -name "*.install" -o -name "*.inc" -o -name "*.module" -o -name "*.test" \) -exec grep -l "$2" '{}' \;

Make sure that /usr/local/bin/search has execute permissions set. Now all you have to do is to use this script to do your text searches. Suppose you have to search for the string "drupal_not_found" in your project located at ~/public_html/drupal. You can do this as follows

search ~/public_html/drupal "drupal_not_found"

Happy grepping :-)