查找基于管道是你的朋友。这个答案假设一个基于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.因为你似乎是一个新用户,如果你得到一个可以帮助你的答案,请记住将它标记为已接受,并且/或者给它一个+(或 - )作为有用的答案。