I'm trying to do a text search in some files that share a similar directory structure, but are not in the same directory tree, in GNU/Linux.
I have a web server with many sites that share the same tree structure (Code Igniter MVC PHP framework), so I want to search in a specific directory down the tree for each site, example:
/srv/www/*/htdocs/system/application/
Where * is the site name. And from those application directories, I want to search all the tree down to its leaves, for an *.php file that has some text pattern inside, let's say "debug(", no regular expression needed.
I know how to use find and grep but I'm not good at combining them.
How would I do this?
Thanks in advance!
Best Answer
Try
This should recursively search the folders under
application
for files with.php
extension and pass them togrep
.An optimization on this would be to execute:
This uses
xargs
to pass all the.php
files output byfind
as arguments to a singlegrep
command; e.g.,grep "debug (" file1 file2 file3
. The-print0
option offind
and-0
option ofxargs
ensure the spaces in file and directory names are correctly handled. The-H
option passed togrep
ensures that the filename is printed in all situations. (By default,grep
prints the filename only when multiple arguments are passed in.)From man xargs: