2011-04-05 114 views
1

我将一个typo3网站(LAMP)从knopix服务器迁移到了ubuntu 10.04机器。现在有一半的网站有点不起作用。我想我找到了问题,但不能解决它,因为我不够数学,所以我可以做好正则表达式的东西。查找并替换多个文件中的文本

在新服务器上的文件是在“/无功/网络/ CMS” 旧服务器/media/sda3/www/quickstart-4.2.1/

让他们我想,如果所有的文件,这些文件在包含子目录的“/ var/www/cms”中,包含“/media/sda1/www/quickstart-4.2.1/”字符串的字符串将替换为该网站将要发布的“/ var/www/cms”正常工作。

帮助将非常感谢这个unix newbe。 在此先感谢!

B

回答

0

查找基于管道是你的朋友。这个答案假设一个基于Linux/Unix的操作系统。

我强烈建议每次只删除一条命令,粘贴到命令行上 除非您有更多的经验,否则不要试图将其转换为shell脚本。 确保你了解每一步都做的和多次检查,结果是他们都应该真的是要和你不希望它是或承担它应该成为什么; - !)

# goto the root of your new filesystem 
cd /var/www/cms 

# print out the filenames with the old path in it 
find . -print | xargs grep -l "/media/sda1/www/quickstart-4.2.1/" 
# find . -print all fileNames 
       # take output from find (via pipe) 
       # xargs manages creation of command line with filenames from pipe 
        # grep -l saarchs for target string and prints only file names 

# make sure there are no files in this list that shouldn't be modified. 

# backup files in list 
find . -print | xargs -I {} /bin/cp {} {}.sav_20110405 
# note that the sav_20110405 is date composed by YYYYMMDD. 
# A good habit to get into 

# run a test on one file from list above 
testFile=/var/www/cms/yourFileNameHere.html 

find . -print -name ${testFile} \ 
| grep -v 'sav_20110405$' \ 
| while read fName ; do 
    ex - ${fName} <<EOS 
    :%s/\/media\/sda1\/www\/quickstart-4.2.1/\/var\/www\/cms/g 
    :wq 
EOS 

done 

这很好,因为使用sed解决方案时,必须管理修补程序的重定向到新文件,然后将固定文件重命名为原始文件。

ex编辑器就像是vi编辑器,但是用于批量输入指令,其好处是您可以发出'写入'命令并'q'退出。

重要 一些shell和or ex命令在这类构造中存在问题。这就是为什么我们要测试一个备份的文件。确保文件正常并且在继续之前备份文件已到位。

# inspect the 'fixed file' and be *really* sure there it is correct 
# also be sure the ${testFile}.sav_20110405 did NOT get modified. 

# run command for all files 
find . -print \ 
| grep -v 'sav_20110405$' \ 
| while read fName ; do 
    ex - ${fName} <<EOS 
    :%s/\/media\/sda1\/www\/quickstart-4.2.1/\/var\/www\/cms/ 
    :wq 
EOS 

done 

在待办事项列表中记下1个月后回来并删除sav文件。

find . -print -name '*.sav_20110405' 

挑一个文件名

find . -print -name 'myTestFile.sav_20110405' | xargs /bin/rm 

确保已删除的唯一文件。

现在清理文件

find . -print -name '*.sav_20110405' | xargs /bin/rm 

好运和三重检查一切;-)的休息!

我希望这会有所帮助。

P.S.因为你似乎是一个新用户,如果你得到一个可以帮助你的答案,请记住将它标记为已接受,并且/或者给它一个+(或 - )作为有用的答案。

0

看起来你不需要这个正则表达式。
这是一个普通的字符串搜索/替换,你可以使用任何文本编辑器。许多文本编辑器不仅可以搜索/替换一个打开的文件,还可以搜索一个洞目录。我经常使用jEdit

另外它应该更容易不直接编辑服务器上的文件,而是使用本地副本处理文件并在文件修复后传输文件。