?
Solved

Search and replace with SSH on all .php and .htm and .html files

Posted on 2009-12-30
13
Medium Priority
?
460 Views
Last Modified: 2013-12-04
Hi, I am using CENTOS 5.4 i686 and can connect with SSH using root. I need to replace this $DOCUMENT_ROOT by that $_SERVER['DOCUMENT_ROOT'] on all files or .php .htm .html

Thank you
0
Comment
Question by:olivierarbez
  • 5
  • 5
  • 3
13 Comments
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26149852
Hi there;

You can use sed for this. Maybe grep will be applied too.

I adapt the answer in the link:
http://www.experts-exchange.com/Operating_Systems/Linux/Q_10515858.html

to your case as in the snippet.

For one file, you can do:
 sed 's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT'] /g' FILE > TMPFILE && mv TMPFILE FILE

Best regards.
cd /
for fl in *.php; do
mv $fl $fl.old
sed 's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT']/g' $fl.old > $fl
rm -f $fl.old
done
cd /
for fl in *.htm; do
mv $fl $fl.old
sed 's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT']/g' $fl.old > $fl
rm -f $fl.old
done
cd /
for fl in *.html; do
mv $fl $fl.old
sed 's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT']/g' $fl.old > $fl
rm -f $fl.old
done

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 26150038
If you have files with no spaces in the name, you can do

 find . -name "*.html" -o -name "*.php" -o -name "*.htm" -exec sed -i  's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT']/g' {} \;
0
 

Author Comment

by:olivierarbez
ID: 26151182
I had to move several websites from a old server to a new server. Many of the pages on several domains have the same php problem.

jazzIIIlove and Tintin: Will this work in all subdirectories?

This will be done on a live server so please make sure the code will work ok.

Thank you

0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
LVL 48

Expert Comment

by:Tintin
ID: 26151226
My solution will work on subdirectories.

If you are nervous about making the changes, you can change my solution to

find . -name "*.html" -o -name "*.php" -o -name "*.htm" -exec sed -i.bak  's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT']/g' {} \;

This will create a copy of all the changed files with a .bak extension.  Once you are happy everything has updated correctly, you can remove the .bak files.
0
 

Author Comment

by:olivierarbez
ID: 26152280
Tintin

.htm files were replace ok and back up
.php files were not replace and not back up

Thanks
0
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 26152322
Sorry, my mistake.  Should be

find . \( -name "*.html" -o -name "*.php" -o -name "*.htm" \) -exec sed -i.bak  's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT']/g' {} \;
0
 

Author Comment

by:olivierarbez
ID: 26152453
Thanks I will try it.

Is it possible to have some type of output that would list all files changed like a output.txt file or a screen output or both?
0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26152501
My solution works in subdirectories too with htm, html and php stuff.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 26152700
jazzIIIlove, your solution will only look in / and not sub-directories.
0
 

Author Comment

by:olivierarbez
ID: 26153909
Tintin is it possible to have some type of output because I have lot of files to check.  A output.txt file would be great.
0
 

Author Closing Comment

by:olivierarbez
ID: 31671441
Would be nice to have a output
0
 
LVL 48

Expert Comment

by:Tintin
ID: 26156149
If you want some additional control/output, use a loop, eg:
#!/bin/bash
find . \( -name "*.html" -o -name "*.php" -o -name "*.htm" \) | while read file
do
  echo "Processing $file"
  sed -i.bak  's/$DOCUMENT_ROOT/$_SERVER['DOCUMENT_ROOT']/g' $file
done

Open in new window

0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26158520
>>Tintin:jazzIIIlove, your solution will only look in / and not sub-directories.
Yes, when I re-recheck the script, there is no recursion in it and yet, the repetition in the code is also something that doesn't look pro.

So, all in all, my script will only work for the files in the / directory, so it's only suitable for files in a directory.

Thanks for the warning and correction Tintin.

Best regards.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Screencast - Getting to Know the Pipeline
Suggested Courses

571 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