shell script to recursively search and find/replace text in target files

Posted on 2011-02-16
Last Modified: 2012-05-11
Hello All,

This is probaably a 1 minute solution for many script maseters, but for me is quite muddy.

1. Recursively search  all child directories for file WP-CONFIG.PHP
2. Find all "Lines" that begin with define('DB_PASSWORD', 'xxxxxx');   -- note 'xxxxxx' can be any length text.
3. Replace the 'xxxxxxx' text with new text that I would supply as paramter argument upon running the script.
4. Produce a count of the number of times the script found and editied a WP-CONFIG.PHP file.

Thats it.
Question by:Louis Capece

Expert Comment

ID: 34907589
#!/usr/bin/env bash

for entry in $(find . -name WP-CONFIG.PHP -a -type f); do
    sed -e "s/^define\('DB_PASSWORD','[^']*'/define('DB_PASSWORD','$REPLACE'/" -i $entry

echo $n

Open in new window

Line 3 takes the first command line argument and puts it in the REPLACE var
Line 5 uses the find utility to locate every file named WP-CONFIG.PHP under the current dir
Line 7 replaces every occurence of ....DB_PASSWORD with the text in REPLACE var


Expert Comment

ID: 34907612
find . -iname "WP-CONFIG.PHP" -print | xargs sed -i 's/oldpass/newpass/g'
grep -lr "newpas" .

Open in new window

Make sure to escape(backslash\ any special characters) and to tell me if there is any other line same as password so I can adapt the script/
LVL 31

Expert Comment

ID: 34907632
or  find . -type f -name WP-CONFIG.PHP
2. grep -R '^define(\'DB_PASSWORD\''

3.  sed "s/xxxxx/yyyyy/g" $1
4.  #Before modifying the script
     Precount=$(grep -c "XXXX" WP-CONFIG.PHP)
     #Edit using above
     Postcount=$(grep -c "XXXX" WP-CONFIG.PHP)
     Edited=$(( Precount - Postcount ))
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.


Author Comment

by:Louis Capece
ID: 34909944
Could someone please confirm this, I suspect there may be confusion...
the existing password is NOT known, it could be anything that is beween the single quotes.

In a nutshell, what I an trying to do is alter all the WP_CONFIG.PHP "Define Password" statement to a single value....the existing passwords are all different.
LVL 12

Expert Comment

ID: 34910642
Hi louiscapece,

de2Zotjes's solution seems to cater for any existing password.
But I don't think it caters for the space you have between the comma and the 'xxxxxx' (old password).  To make it flexible in this regard (i.e. to handle 0 or more spaces there), maybe change line 7 to something like this:
    sed -e "s/^define\('DB_PASSWORD', *'[^']*'/define('DB_PASSWORD', '$REPLACE'/" -i $entry

Accepted Solution

florjan earned 500 total points
ID: 34910645
find . -iname "WP-CONFIG.PHP" -print | xargs sed -i "s/('DB_PASSWORD', '.*')/('DB_PASSWORD', 'newpassword')/g"
grep -lr "newpas" .

Open in new window


Expert Comment

ID: 34910669
I have tested script from my last post on gentoo on my test wp install in bash and it worked.

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now