使用Find,Grep,Awk或Sed来克隆后重命名服务器

我的客户告诉我他们已经在Ubuntu Linux服务器的VMWare中克隆了一个虚拟机。 现在,我的工作是进入所有的文件,并找出还有什么旧的服务器名称“主教”,并将其改为别的。 此外,IP地址已更改,我也需要search。

你通常如何使用findgrepawksed来查找这些文件,然后迅速更改它们? 最后,我想制作一个Bash脚本。

当然,我不希望你告诉我每一个文件,但只是想知道find“X”文件的技术,然后用“Y”快速切换。

我实际上强烈build议你做这两步操作:

  1. find旧主机名和旧IP的文件:

    find / -print | xargs egrep -l 'oldhost|10.10.10.10' > filelist

  2. 然后编辑结果列表,并删除任何你知道不应该改变的东西。

  3. 然后使用结果列表,将其放在shell脚本中,并使用filelist作为input,执行类似以下命令序列的操作:

 cp filename filename.20110624 # lets be safe! if test $? -ne 0 then echo 'filename copy bad' exit 1 fi cat filename.20110624 | sed 's/oldhost/newhost/g s/10.10.10.10/10.20.20.20/g' > filename # the newline between commands is needed if test $? -ne 0 then echo edit failed cp filename.20110624 filename if test $? -ne 0 then echo unable to restore filename exit 1 fi exit 1 fi exit 0