I changed my password and username for mySQL and I need to replace the connection string in all of my PHP scripts accordingly.

I am having trouble with the command because many of my php file names contain spaces.

What can I change to make this command work without having "No such file or directory" errors?

Here is the command I am using:

pattern='mysql_connect("localhost", "olduser", "oldpwd")'
replacement='mysql_connect("localhost", "newuser", "newpwd")'
find . -name "*.php" | xargs -n 1 sed -i -e 's|$pattern|$replacement|g'

I am unfamiliar with xargs and sed and I copied this code from this answer to a similar question.

(I think I will put the mysql_connect statement into a php include file and only change it in one place next time.)

I'm running Ubuntu 14.04 LTS using BASH

  • I know this is going to be something simple, but I'm old and I can't remember. Heh. May 30, 2020 at 1:01
  • Something like ${i##*} ? May 30, 2020 at 1:04

2 Answers 2


You have to use the NULL byte and double quotes :

find . -name '*.php' -print0 |
    xargs -0 -n 1 sed -i -e "s|$pattern|$replacement|g"

Or with a simple for loop:

for p in *php; do
    sed -i -e "s|$pattern|$replacement|g" "$p"

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See

  • I knew it was going to be something simple. Thanks! I'll accept your answer so you get the reputation. May 30, 2020 at 1:22

I was able to find success by replacing the values like so:

find . -name "*.php" | xargs -n 1 sed -i -e 's/\"olduser\", \"oldpwd\"/\"newuser\", \"newpwd\"/g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.