1

I would like to know how to find and replace text in all HTML, htm, PHP and txt extension files on Linux server at hosting provider. I can do SSH.

Text to be found:http://mydomain.example.com

Text to be replaced with:http://otherdomain.example.com/MyDomain

Please give me exact command and it would be much helpful if it can prompt for confirmation before replacement.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Amit Bhagat
  • 4,182
  • 3
  • 23
  • 24
  • 1
    Look at the `sed` command, or maybe `perl` or `awk`. http://stackoverflow.com/questions/12663501/find-string-and-replace-line-in-linux http://stackoverflow.com/questions/8384809/replace-pattern-in-text-file for starters. – Andy Lester Dec 16 '12 at 05:58
  • 1
    possible duplicate of [Linux command line global search and replace](http://stackoverflow.com/questions/471183/linux-command-line-global-search-and-replace) – Nir Alfasi Dec 16 '12 at 06:07

2 Answers2

3

Here's one way using sed and a for loop. Use the -i flag with mv to prompt before overwrite:

for i in *.html *.htm *.php *.txt; do sed 's%\(http://www\.\)\(MyDomain\)\(\.com\)%\1OtherDomain\3/\2%g' "$i" > tmp && mv -i tmp "$i"; done
Steve
  • 51,466
  • 13
  • 89
  • 103
0
sed -i 's/http:\/\/www.mydomain.com/http:\/\/www.otherdomain.com\/MyDomain/gi' *.txt *.html *.htm *.php
Tad
  • 934
  • 6
  • 10