views:

367

answers:

3

How can I grep for a certain string recursively in all .htaccess files?

grep -r -n -H -I 'string' .htaccess

doesn't seem to work.

I'm on a GNU Linux system.

A: 

Use the --include option:

grep -rnHI 'pattern' --include=.htaccess .
Adam Rosenfield
+2  A: 

cd to a folder before the folders that store the htaccess

$find . -name ".htaccess" -exec grep -r -n -H -I 'string' {} \;
Ryan Fernandes
no need to cd: $ find dir -name ...
William Pursell
find is a powerful way to walk a directory tree, the exec flag is a nice way to run commands against the results of the find command.
Jim
+1  A: 

You can also specify to 'find' that it needs to look for regular files only:

  $ find /usr/local -type f -name ".htaccess" -exec grep -rnHI 'pattern' {} \;

You can specify from where your search should begin. For this example, 'find' will look into all directories and sub directories under /usr/local.

Barun