Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1278232
  • 博文数量: 213
  • 博客积分: 7590
  • 博客等级: 少将
  • 技术积分: 2185
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-31 17:31
个人简介

热爱开源,热爱linux

文章分类

全部博文(213)

文章存档

2018年(4)

2017年(1)

2015年(1)

2014年(5)

2013年(2)

2012年(2)

2011年(21)

2010年(82)

2009年(72)

2008年(23)

分类: LINUX

2010-05-24 21:06:10

1.上次面试面试官出了一个将一个指定文件的后缀名修改为指定后缀的脚本题目,当时没有完成,最近找了点资料用sed实现了一下,如下面代码是实现将一个文件的后缀名修改为指定后缀的脚本

  1 #!/bin/bash
  2 #
  3 # Script name:change_suffix.sh
  4 # Author: CaoJiangfeng
  5 # The script is used to rename a file to a new file with given suffix
  6 # such as change a file hello.c to file hello.cpp we can use as this
  7 # bash change_suffix hello.c cpp or ./change_suffix.sh hello.c cpp
  8 # then the hello.c becomes to hello.cpp
  9 #
 10 echo $#
 11 if [ $# -lt 2 ] ; then
 12 echo "Usage:./script.sh filename suffix"
 13 exit 1
 14 fi
 15 filename=$1
 16 suffix=$2 #得到新文件的后缀
 17 newfilename=`echo $filename|sed -r 's/(.*)(\..*)/\1/g'` #该语句实现将filenam e的后缀与文件名分割开
 18 mv $filename $newfilename.$suffix



2.上面脚本只能每次将一个文件的后缀进行修改,下面脚本实现文件批处理修改文件后缀,实现之前,现了解一下rename的用法:
#rename命令是Linux下功能非常强的的重命名工具,利用正则表达式使得其使用地非常灵活。
#rename命令的格式:
#rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
#-v, --verboseVerbose: print names of files successfully renamed.
#-n, --no-actNo Action: show what files would have been renamed.
#-f, --forceForce: overwrite existing files.
#简单的rename使用命令:
#字母的替换
#rename "s/AA/aa/" *             //把文件名中的AA替换成aa
#修改文件的后缀
#rename "s/\.html/\.php/" *     //把.html 后缀的改成 .php后缀
#批量添加文件后缀
#rename "s/$/\.txt/" *             //把所有的文件名都以txt结尾
#批量删除文件名
#rename "s/\.txt//" *               //把所有以.txt结尾的文件名的.txt删掉

下面是实现文件名后缀名批量修改的一个例子:

1 #!/bin/bash
  2 #
  3 # Script name:rename_suffix.sh
  4 # Author :CaoJiangfeng
  5 #
  6 # The script is used to rename current directory files which have the
  7 # given suffix to new suffix such as in a directory has files 1.c and
  8 # 2.c ,the oldsuffix is c while current given suffix is cpp,we can use
  9 # the script to rename them like this :./rename_suffix.sh c cpp ;After
 10 # that the 1.c 2.c becomes to 1.cpp 2.cpp
 11 #
 12 if [ $# -lt 2 ] ;then
 13 echo "Usage:./script_name source_suffix target_suffix "
 14 exit 1
 15 fi
 16 source_suffix=$1
 17 target_suffix=$2
 18 rename -v "s/\.$source_suffix/\.$target_suffix/" *

3,实现文件后缀名批量修改还可以用以下脚本

1 #!/bin/bash
  2 #
  3 # Script name:change_suffix.sh
  4 # Author :CaoJiangfeng
  5 # The script is used to rename the *.c files in current directory
  6 # to *.cpp files
  7 #
  8 if [ $# -lt 2 ] ; then
  9 echo "Usage:./script_name source_suffix target_suffix"
 10 exit 1
 11 fi
 12 source_suffix=$1
 13 target_suffix=$2
 14 for i in *.$source_suffix;
 15 do
 16 mv -f "$i" ${i%%.$source_suffix}.$target_suffix;
 17 done



4下面脚本也可以实现



1 #!/bin/bash
  2
  3 Usage()
  4 {
  5 echo "Usage : script source_suffix target_suffix"
  6 echo " 后缀名为空时用#表示"
  7 exit 1
  8 }
  9 if [ $# -ne 2 ];then
 10 Usage
 11 fi
 12
 13 source_suffix="\.$1"
 14 target_suffix="\.$2"
 15 echo $source_suffix
 16 #if $1 is "#",then add a suffix($2) to each file
 17 if [ "$1" = "#" ]; then
 18 {
 19 source_suffix=""
 20 }
 21 fi
 22 #if $2 is "#" ,then delete every file's suffix($1)

 23 if [ "$2" = "#" ]; then
 24 {
 25     target_suffix=""
 26 }
 27 fi
 28
 29 for file in *$source_suffix
 30 do
 31     if [ -f $file ];then
 32         newfile=`echo $file |sed "s/$source_suffix\$/$target_suffix/g"`
 33         #mv $file `echo $file |sed "s/$1\$/$2/g"`;
 34         mv $file $newfile
 35         echo "$file ==> $newfile"  
 36     fi
 37 done
 38 echo "Successful change names!"  $2 is "#" ,then delete every file


5下面脚本实现对文件名的顺序递增重命名,该脚本实现将$1目录下的图片按照顺序重命名;

1 #!bin/bash
  2 $dir=$1
  3 A=1215;
  4 for file in $1/*; do
  5
  6 echo $file;
  7 if [ -f $file ]; then
  8 mv $file DSC0$A.jpg;
  9 fi
 10 A=`expr $A + 1`;
 11 done
~


6.还有一个最简单的方法,就是利用find和rename命令对指定目录下的指定文件后缀的文件进行重名名
如下:
将当前目录下的所有以java为后缀的修改为cpp后缀

find ./ -print -exec rename -v 's/\.*.java$/.cpp/' {} \;



7.使用find加循环批量递归搞一下
  1. #!/bin/bash
  2. suffix=$1
  3. new_suffix=$2
  4. echo $suffix
  5. for i in `find . -name "*.${suffix}"`
  6. do
  7.         echo "$i ===> ${i%%*.}.${new_suffix}"
  8.         echo  $i ${i%%*.}.${new_suffix}
  9. done
阅读(9201) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~