最近在测试环境维护时,找看到几个批量修改目录和文件名以及文件内容的脚本文件,贴出来分享下
一 修改目录 rename_dir.sh
- #!/bin/sh
- #
- if [ $# != 3 ]; then
- echo "missing operand"
- echo "Usage: rename.sh srcstr deststr path"
- echo "Sample: rename.sh from to ."
- exit 0
- fi
- str1="$1"
- str2="$2"
- path="$3"
- for f in $(find "$path" -type d)
- do
- fn=`echo ${f} | grep ${str1}`
- if [ $? == 0 ]; then
- fn=`echo ${f} | sed -e "s/${str1}/${str2}/g"`
- mv ${f} ${fn}
- echo "${f} --->>> ${fn}"
- fi
- done
- echo "all done!"
二 修改文件名 rename_file.sh
- #!/bin/sh
- #
- if [ $# != 4 ]; then
- echo "missing operand"
- echo "Usage: rename.sh srcstr deststr path file"
- echo "Sample: rename.sh from to . *.sh"
- exit 0
- fi
- str1="$1"
- str2="$2"
- path="$3"
- file="$4"
- for f in $(find "$path" -name "$file" -type f)
- do
- fn=`echo ${f} | grep ${str1}`
- if [ $? == 0 ]; then
- fn=`echo ${f} | sed -e "s/${str1}/${str2}/g"`
- mv ${f} ${fn}
- echo "${f} --->>> ${fn}"
- fi
- done
- echo "all done!"
三 修改文件中的字符串 replace_in_file.sh
- #!/bin/sh
- #
- if [ $# != 4 ]; then
- echo "missing operand"
- echo "Usage: replace_in_file.sh srcstr deststr path file"
- echo "Sample: replace_in_file.sh 192.168.0.1 xxx.xxx.xxx.xxx . *.sh"
- exit 0
- fi
- str1="$1"
- str2="$2"
- path="$3"
- file="$4"
- for f in $(find "$path" -name "$file" -type f | xargs grep -l "$str1")
- do
- fn="${f}.$(date +%s)"
- cp -f "$f" "$fn"
- sed -e "s/$str1/$str2/g" $fn > $f
- rm -rf $fn
- echo "${f} replaced"
- done
- echo "all done!"
阅读(1806) | 评论(0) | 转发(0) |