Search and Replace in SSH

Getting a lot of exposure in Linux environments lately.

Used “top” to see running processes, “tail -f access_log” to watch accesses to my websites. Updated the iptable to block access from a specific IP address that’s been making requests to wpad.dat on one of my servers.

And now, running a long command to update the MySQL username and password details in .php files for a website with many subdomains.

I don’t have the patience to go through every subdomain in an FTP client to update those details so I googled a bit and requested root access to the server and ran:

find . -name \*.php -type f -exec sed -i ‘s/\$MYSQL_PASSWORD = \”oldpassword\”/\$MYSQL_PASSWORD = \”newpassword\”/g’ {} +

It’s probably not the best way but it’s the only way I can find at the moment.

What it does is to go through all files and folders, and their subfolders in the current directory (I’ve already navigated my way to the www/ dir), look for files with the .php extension, and execute the sed command on those files.

The sed command then looks for the string $MYSQL_PASSWORD = “oldpassword” and replaces it with the new string $MYSQL_PASSWORD = “newpassword”. I could omit the $MYSQL_PASSWORD string but the previous person in charge had used the same string for the username and password.

The first time I ran the command to update the username, I did not specify a file extension and the command took about 10 minutes to finish. It was then I found out that there were log files that are about 1.6GBs in size.

The second time I ran it, I specified .php and it finished in less than a minute.

That was fun.

Leave a Reply

Your email address will not be published. Required fields are marked *