• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

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
  • 2
1 Solution
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!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now