grep to display folders which contain files which contain a string
I need to run a grep on my server to search for files which extend a CodeIgniter 1 file, as we are upgrading to CI2. In one folder contains several hundred site-specific folders like such:
dev/sitea.com/site/www
dev/siteb.com/site/www
dev/sitec.com/site/www
... and so on
How can I grep to find which folders directly under dev contain a file (or many files in variable subfolders) which references the string "MY_Output" (php file)
I'm aware of the -l
flag to list the files, but I'm not sure how to put it with other flags together to say "show me which websites contain files with this string"
Is this possible? Thanks!
EDIT: Just to clarify, these site level folders will contain files which extend MY_Output.php, which is a common shared file:
class Whatever extends MY_Output
This would be my approach:
find dev -type f -print0 | \ # find all files
xargs -0 grep 'extends MY_Output' | \ # search for your string
cut -d/ -f2 | \ # extract web folder name
sort | uniq # eliminate duplicates
Note use of the print0
parameter to find
and the -0
(zero) flag to xargs
, which prevents problems if your filenames have embedded spaces in them.
I see a lot of needless complexity in the various solutions posted. Consider the following:
grep -r [regex to find] [path to search] | awk -F: '{print $1}' | uniq