简体   繁体   English

如何在Linux服务器上查找和替换HTML,htm,PHP和txt文件中的文本?

[英]How can I find & replace text in HTML, htm, PHP and txt files on a Linux server?

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. 我想知道如何在托管服务提供商的Linux服务器上查找和替换所有HTML,htm,PHP和txt扩展文件中的文本。 I can do SSH. 我可以做SSH。

Text to be found : http://mydomain.example.com 可以找到的文字http://mydomain.example.comhttp://mydomain.example.com

Text to be replaced with : http://otherdomain.example.com/MyDomain 文本替换为http://otherdomain.example.com/MyDomain : http://otherdomain.example.com/MyDomain

Please give me exact command and it would be much helpful if it can prompt for confirmation before replacement. 请给我确切的命令,如果它可以提示您在更换前进行确认,这将非常有帮助。

Here's one way using sed and a for loop. 这是使用sedfor循环的一种方法。 Use the -i flag with mv to prompt before overwrite: 在覆盖前使用-i标志和mv提示:

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
sed -i 's/http:\/\/www.mydomain.com/http:\/\/www.otherdomain.com\/MyDomain/gi' *.txt *.html *.htm *.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM