After some testing on a server with a strange setup I needed to find all of the .htaccess files that might contain a particular php configuration directive. The pattern was unique enough that I would have probably just done a recursive grep for it in any other circumstance. But by default “grep -R someSearchString *” would ignore dot files like .htaccess, and on a bigger server this could have grep looking through a lot of files when I knew I only wanted to search .htaccess files. “grep -R someSearchString .*” would be just bad since it would recurse into the ‘..’ directory, so you would end up searching your whole harddrive.
The solution? “find”
1 2 3 | $ cd /home $ sudo find ./ -iname ".htaccess" -exec grep -H some_php_config {} \; ./someuser/httpdocs/.htaccess: some_php_config some_value |
Sudo runs the command as root, since I’m looking in everyones’ home directories. find is the command that is actually looking for the files. Then it passes the matches to grep. The -H flag on grep gives you the name of the files, without it you would just get the matching lines from the files, but no indication which files they were in.
Just find all .htaccess files
In some cases it’s just nice to be able to find all of the .htaccess files
1 2 3 4 | $ sudo find ./ -iname ".htaccess" -exec echo {} \; ./someuser/httpdocs/config/.htaccess ./someuser/httpdocs/.htaccess ./someuser/httpdocs/.htaccess |