批量将大写文件名转换成小写

发表于:2007-06-08来源:作者:点击数: 标签:
master于7.2917:54整理 首先介绍一下tr命令的一个用法trstring1string2, string1/2指定同等数量的字符集合,输入字符在string1中 寻找,若找到,则输出时替换成string2中相应位置的字符。 #!/bin/csh if($#argv!=1nbsp;then echo"Usage:$0-l|-u" exit1 endi

master于7.29 17:54整理
 


首先介绍一下tr命令的一个用法tr string1 string2, 
string1/2指定同等数量的字符集合,输入字符在string1中 
寻找,若找到,则输出时替换成string2中相应位置的字符。 
#! /bin/csh 
if ( $#argv != 1  then 
    echo " Usage: $0 -l|-u " 
    exit 1 
endif 
if ( "$1" != "-l" && "$1" != "-u"  then 
    echo " Usage: $0 -l|-u " 
    exit 1 
endif 
if ( "$1" == "-l"  then 
    foreach file ( *  
        mv $file `echo $file | tr '[A-Z]' '[a-z]'` 
    end 
else 
    foreach file ( *  
        mv $file `echo $file | tr '[a-z]' '[A-Z]'` 
    end 
endif 
  
对于ksh,介绍一下typeset -l varname命令, 
这个命令的意思是$varname中的所有字符 
将被转换成小写。相应的是-u参数。 
#! /bin/ksh 
if [ $# -ne 1 ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
if [ $1 != "-l" -a $1 != "-u" ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
typeset $1 targetFile 
for file in * 
do 
    targetFile="$file" 
    mv $file $targetFile 
done 
  
再介绍一下expr string1 : string2命令,string1是待处理 
字符串,string2是一个正则表达式,输出将是匹配处理后的结果。 
之所以要多费点手脚,因为可能文件名目录名存在特殊字符, 
比如空格等,expr string1 : string2这个命令不管文件名目 
录名里有没有特殊字符都会输出原始名称。 
#! /bin/sh 
if [ $# -ne 1 ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
if [ $1 != "-l" -a $1 != "-u" ] 
then 
    echo " Usage: $0 -l|-u " 
    exit 1 
fi 
if [ "$1" = "-l" ] 
then 
    for file in * 
    do 
        targetFile=`expr "+++$file" : '+++\(.*\)' | tr '[A-Z]' '[a-z]'` 
        mv "$file" "$targetFile" 
    done 
else 
    for file in * 
    do 
        targetFile=`expr "+++$file" : '+++\(.*\)' | tr '[a-z]' '[A-Z]'` 
        mv "$file" "$targetFile" 
    done 
fi 
这里所有的示例都假设只处理了当前子目录下的本层的文件, 
如要进行目录树的处理,可以配合使用find命令。上面的 
+++仅仅是为了便于匹配处理排除一些干扰因素,没有别的 
实际意义。

原文转自:http://www.ltesting.net