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