I’m attempting to find all .PHP files that are in certain depth of a directory (at least 4 levels down, but not more than 5 levels in).
I’m logged into my Centos server with root authority via shell.
The string I want to search for is:
$slides='';
What I have in front of me.. I would expect it to work. I tried to escape the $ with a (I thought perhaps it works like regex, needing special chars excluded). I tried without the =''
portion, or tried adding ''
to that part.. or remove the =''
altogether to simplify. nothing.
find . -maxdepth 5 -mindepth 4 -type f -name ‘*.php’ -print | xargs grep "$slides=’’" *
I’m already running it under the directory under which I want to recursively search.
Also – I have the filter to look for *.php only but I still get a bunch of directory names in the return with a warning that says grep: [dir_name]: Is a directory
Clearly I am missing something here as far as syntax of grep command goes, or how the filter works here. I use grep more in PHP so this is quite a transition for me!
2
Answers
If you want to use shell_exec from your PHP code, it is a program execution function which allows you to run a command like ‘ls -al’ in the operating system shell and get the result returned into a variable. Querystrings are not commands you can use in this way.
Do you mean running PHP from the command line so that it runs from the shell, not from the web server:
php -r ‘echo "hello worldn";’
If you run PHP 4.3 and above, you can use the PHP Command Line Interface (CLI) which can also execute scripts stored in files. Have a look at the syntax and examples at: http://php.net/features.commandline
So you were almost right. The problem looks to have been the
grep
part of the commandNamely the
*
was the issue. From the bash manualWhen you piped the found files with
find
intoxargs
and attempted togrep
them with*
,grep
would have interpreted this as you wanting to find the string$slides=''
in a list of filenames/directories returned by the glob*
, and you cannotgrep
directories without supplying the-r
flag togrep
, so it returned an error.Instead, what you wanted to do is pipe the found files with
find
intoxargs
so it can add the list of filenames to thegrep
command, as that’s whatxargs
does. From the xargs manualMaking the correct command
Using the
-print0
flag infind
, and the-0
flag inxargs
, to use NUL as the delimiter, in case any filenames contained newlines.