Linux: Find All PHP Scripts Containing MySQL Queries Without Errors

I use the following code to search for PHP scripts containing the specified text. (Thanks, woolmilkporc.)
find /opt /home  \( -name "*.php" -o -name "*.php[3-6]"  -o -name "*.inc" \) | xargs grep -Eil "mysql|select|result" 2>/dumpErrors.txt>dumpfile.txt

Open in new window

There are two problems.

1.  "Permission denied" errors are not being logged to "dumpfile.txt".
2.   Some files are generating grep errors because of the file name.  For example, this file:
/home/mydir/Copy of myscript.php

Causes this error:
grep: /home/mydir/Copy: No such file or directory
LVL 16
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi again,

the "Permission denied" errors should indeed be logged when using our code.
Are you sure that there are any? I see that you're restricting the search to
some well-defined directories as I suggested -
could it be that you do have full access to these directories so that no errors are thrown?

The second requirement can be met like this:  
find /opt /home  \( -name "*.php" -o -name "*.php[3-6]"  -o -name "*.inc" \) | xargs -I{} grep -Eil "mysql|select|result" "{}" 2>/dumpErrors.txt >dumpfile.txt

Open in new window

Please note the -I{} option to xargs and the "{}" parameter of grep.


now I see my mistake regarding the error messages!

They don't come from "grep", they're coming from "find".

Find the corrected version attached!
find /opt /home  \( -name "*.php" -o -name "*.php[3-6]"  -o -name "*.inc" \) 2>/dumpErrors.txt | xargs -I{} grep -Eil "mysql|select|result" "{}" 2>>/dumpErrors.txt >dumpfile.txt

Open in new window

Please note the additional redirect to the errorlog for "find" and the double ">>" in the redirect for "grep"

Sorry again!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.