使用方式如下:
frall.sh /work teststr testok
执行结果如下:
/work目录下所有(包括子下)中存在teststr字符串的位置,全部被testok字符串所替代.
代码:#!/bin/sh
useage()
{
echo "useage:$0 dirname oldstr newstr"
echo "attantion:dirname must haven't oldstr!"
}
tf1=/tmp/.f.tmp1
tf2=/tmp/.f.tmp2
workdir=`pwd`
rm -f $tf1
rm -f $tf2
# do with /$2-xxx and /$2-xxx/$2-yyy
if [ $# -eq 0 ];then
useage
exit
fi
for dir in `find $1/* -type d`
do
echo $dir | awk 'BEGIN {FS="/"} ;{print $NF}' | grep $2
if [ $? = 0 ];then
echo `echo $dir | wc -c ` $dir >> $tf1
fi
done
sort -r -n $tf1 -o $tf2
while read LINE
do
dir=`echo $LINE | cut -d " " -f 2`
olddir=`echo $dir | awk 'BEGIN {FS="/"};{print $NF}'`
newdir=`echo $olddir | s/$2/$3/g`
cd $dir;cd ..
mv $olddir $newdir
cd $workdir
done < $tf2
for file in `find $1/* -type f`
do
grep $2 $file 1>2 2>/dev/null
if [ $? = 0 ] && [ $file != $0 ]; then
# echo "$file have $2 " >> /tmp/nnn
ed - $file << EO
g/$2/s/$2/$3/g
.
w
q
EO
fi
newname=`echo $file | sed s/$2/$3/g`
mv $file $newname
done