Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153716
  • 博文数量: 17
  • 博客积分: 357
  • 博客等级: 一等列兵
  • 技术积分: 706
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-12 16:49
文章分类

全部博文(17)

文章存档

2013年(3)

2012年(14)

我的朋友

分类: LINUX

2012-09-23 21:50:13

最近在测试环境维护时,找看到几个批量修改目录和文件名以及文件内容的脚本文件,贴出来分享下
 
一 修改目录 rename_dir.sh

点击(此处)折叠或打开

  1. #!/bin/sh
  2. #
  3. if [ $# != 3 ]; then
  4. echo "missing operand"
  5. echo "Usage: rename.sh srcstr deststr path"
  6. echo "Sample: rename.sh from to ."
  7. exit 0
  8. fi
  9. str1="$1"
  10. str2="$2"
  11. path="$3"
  12. for f in $(find "$path" -type d)
  13. do
  14. fn=`echo ${f} | grep ${str1}`
  15. if [ $? == 0 ]; then
  16. fn=`echo ${f} | sed -e "s/${str1}/${str2}/g"`
  17. mv ${f} ${fn}
  18. echo "${f} --->>> ${fn}"
  19. fi
  20. done
  21. echo "all done!"

二 修改文件名 rename_file.sh

点击(此处)折叠或打开

  1. #!/bin/sh
  2. #
  3. if [ $# != 4 ]; then
  4. echo "missing operand"
  5. echo "Usage: rename.sh srcstr deststr path file"
  6. echo "Sample: rename.sh from to . *.sh"
  7. exit 0
  8. fi
  9. str1="$1"
  10. str2="$2"
  11. path="$3"
  12. file="$4"
  13. for f in $(find "$path" -name "$file" -type f)
  14. do
  15. fn=`echo ${f} | grep ${str1}`
  16. if [ $? == 0 ]; then
  17. fn=`echo ${f} | sed -e "s/${str1}/${str2}/g"`
  18. mv ${f} ${fn}
  19. echo "${f} --->>> ${fn}"
  20. fi
  21. done
  22. echo "all done!"

 
三 修改文件中的字符串 replace_in_file.sh
 

点击(此处)折叠或打开

  1. #!/bin/sh
  2. #
  3. if [ $# != 4 ]; then
  4. echo "missing operand"
  5. echo "Usage: replace_in_file.sh srcstr deststr path file"
  6. echo "Sample: replace_in_file.sh 192.168.0.1 xxx.xxx.xxx.xxx . *.sh"
  7. exit 0
  8. fi
  9. str1="$1"
  10. str2="$2"
  11. path="$3"
  12. file="$4"
  13. for f in $(find "$path" -name "$file" -type f | xargs grep -l "$str1")
  14. do
  15. fn="${f}.$(date +%s)"
  16. cp -f "$f" "$fn"
  17. sed -e "s/$str1/$str2/g" $fn > $f
  18. rm -rf $fn
  19. echo "${f} replaced"
  20. done
  21. echo "all done!"

阅读(1733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~