UNIX find and replace inside php files

Discussion in 'Site & Server Administration' started by Kendothpro, Mar 25, 2007.

  1. #1
    I have a big list of php files that need a hardcoded path changed
    So let's say I want to change /mydir/file.php to /yourdir/file.php, in all the .php files under /home/mydomains/

    There must be an easy one-line way using grep or find in UNIX, could anyone suggest it please?
     
    Kendothpro, Mar 25, 2007 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    I know it isn't what you asked.

    You can do it under windows using multi-replace. The downside is you have to add all the files to the list.
     
    Colbyt, Mar 25, 2007 IP
  3. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Thanks but I have linux and 78 websites on which I'd like to change that one line, so I have no other options :/

    After some searching I've tried the following:

    find /home/admin/domains/mydomain.com/public_html/ -type f | xargs perl -pi -e 's/include\(\'\.\.\/inc\/stuff\.php\'\);/include\(\'\/mylibs\/stuff\.php\'\);/g'

    When I use it in SSH though, I get a prompt (??) that apparently is waiting for my input..any idea?
    The escapes seem to be ok
     
    Kendothpro, Mar 25, 2007 IP
  4. maonnie

    maonnie Member

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    try:

    find /home/admin/domains/mydomain.com/public_html/ -type f | xargs perl -pi -e "s/include\(\'\.\.\/inc\/stuff\.php\'\);/include\(\'\/mylibs\/stuff\.php\'\);/g"

    ;)
     
    maonnie, Mar 25, 2007 IP
  5. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #5
    lol thanks it worked :)
     
    Kendothpro, Mar 25, 2007 IP
  6. rootbinbash

    rootbinbash Peon

    Messages:
    2,198
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ls -al /home/asdasd/bsfdsfsd/ | grep .php | xargs mv /home/asdkasdh/mnmsandabs
     
    rootbinbash, Mar 26, 2007 IP
  7. databit

    databit Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hey stumbled on this while googling what I was trying to do.
    I need to do the same thing but having "issues"
    I need to do a find and replace in all ascii files in the current directory and recursively through the sub directories I need to replace
    </body>

    with
    </body><test string here>
    but when I try running

    find -type f | xargs perl -pi -e "</body>\s+;</body><test\s+string\s+here>'\);/g"

    I get:
    Backslash found where operator expected at -e line 1, near "</body>\"
    (Missing operator before \?)
    Bareword found where operator expected at -e line 1, near "s+;</body><test\s+string\s+here"
    syntax error at -e line 1, near "</body>\"
    Can't find string terminator "'" anywhere before EOF at -e line 1. ​
     
    databit, Apr 10, 2007 IP
  8. databit

    databit Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Actually just found the answer myself
    find /testdir -type f -exec sed -i 's#</body>#</body><string for you>#g' {} \;

    works like a champ
     
    databit, Apr 11, 2007 IP