skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. So you were almost right. The problem looks to have been the grep part of the command

    grep "$slides=''" *
    

    Namely the * was the issue. From the bash manual

    After word splitting, unless the -f option has been set (see The Set
    Builtin), Bash scans each word for the characters ‘*’, ‘?’, and ‘[’.
    If one of these characters appears, and is not quoted, then the word
    is regarded as a pattern, and replaced with an alphabetically sorted
    list of filenames matching the pattern

    When you piped the found files with find into xargs and attempted to grep 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 cannot grep directories without supplying the -r flag to grep, so it returned an error.

    Instead, what you wanted to do is pipe the found files with find into xargs so it can add the list of filenames to the grep command, as that’s what xargs does. From the xargs manual

    xargs reads items from the standard input, delimited by blanks (which
    can be protected with double or single quotes or a backslash) or
    newlines, and executes the command (default is /bin/echo) one or more
    times with any initial- arguments followed by items read from
    standard input. Blank lines on the standard input are ignored.

    Making the correct command

    find . -maxdepth 5 -mindepth 4 -type f -name '*.php' -print0  | xargs -0 grep "$slides=''"
    

    Using the -print0 flag in find, and the -0 flag in xargs, to use NUL as the delimiter, in case any filenames contained newlines.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search