linux 搜索並替換文件內容

1、指定目錄 terry 下的所有文件內容中,把 terry 替換爲 breakeast ,此處爲 正則替換,需要進行轉義處理

find ./terry -type f | xargs perl -i -pe s%terry%breakeast%g

 

2、指定替換文件類型

find ./terry "*.html" -type f | xargs perl -i -pe s%terry%breakeast%g

 

3、若要替換時,同時生成備份,則需要寫shell腳本實現:

#!/bin/bash
str="www.breakeast.com"
newstr="blog.breakeast.com"
for i in `find ./terry/`
do
grep "$str" $i
if [ $? == 0 ]
then
echo $i

cp $i $i.newfile
sed "s/$str/$newstr/g" $i 
fi
done

源於青互聯博客http://www.qing.es

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章