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