查找基於管道是你的朋友。這個答案假設一個基於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.因爲你似乎是一個新用戶,如果你得到一個可以幫助你的答案,請記住將它標記爲已接受,並且/或者給它一個+(或 - )作爲有用的答案。