This is a website by Willem van Zyl

I'm a project manager, software developer, Apple evangelist and geek from South Africa. I'm passionate about web and mobile application development, usability, productivity, physics, astronomy, science fiction and fantasy.

If you would like to contact me, message me on Twitter or send me an email.

How to find text within specific types of files from the command line

09 Sep 2010

Sometimes it's useful to speed up text (or regular expression)-based searches from the command line by only searching through specific types of files.

To find text within specifically-typed files from the command line, open a new Terminal window, switch to the directory containing the files you want to search through, and enter the following command (replacing "js|php|sh" with a pipe-separated list of file extensions and "willem" with your search string):

find . -type f | egrep 'js|php|sh' | sed -e "s@'@\\\'@g" -e "s@ @\\\ @g" | xargs grep "willem"

This command will recursively find all files within the current directory, filter only files with the specified extensions from the list, and search each of these files for the specified search text.

After the command has run, you'll see output similar to the following:

./system/application/config/acl.php:  * @author     Willem van Zyl (willem@geekology.co.za)
./system/application/config/acl.php:  * @version    $Id: acl.php 1030 2010-07-19 09:49:01Z willem $
./system/application/config/autoload.php:  * @author     Willem van Zyl (willem@geekology.co.za)
./system/application/config/autoload.php:  * @version    $Id: autoload.php 930 2010-06-23 13:33:11Z willem $
./system/application/config/config.php:  * @author     Willem van Zyl (willem@geekology.co.za)
./system/application/config/config.php:  * @version    $Id: config.php 1008 2010-07-09 13:55:42Z willem $
./system/application/config/constants.php:  * @author     Willem van Zyl (willem@geekology.co.za)
./system/application/config/constants.php:  * @version    $Id: constants.php 930 2010-06-23 13:33:11Z willem $
Do you like this? Share it:

Copyright © Geekology 2011. All Rights Reserved.

Hosted by Code. Like. Clockwork.