2011-04-05 149 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

另外它應該更容易不直接編輯服務器上的文件,而是使用本地副本處理文件並在文件修復後傳輸文件。