基于查找的管道是您的朋友。这个答案假设一个基于 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 编辑器,但用于批量输入指令,好处是您可以发出“write”命令和“q”退出。
重要
某些 shell 和/或 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
祝你好运并三重检查一切;-)!。
我希望这有帮助。
PS,因为您似乎是一个新用户,如果您得到一个对您有帮助的答案,请记住将其标记为已接受,和/或给它一个 +(或 -)作为有用的答案。