免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3515 | 回复: 2
打印 上一主题 下一主题

[SCO UNIX] 批量将大写文件名转换成小写 [复制链接]

论坛徽章:
1
15-16赛季CBA联赛之北控
日期:2022-03-04 22:35:50
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-02-19 23:26 |只看该作者 |倒序浏览
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命令。上面的
+++仅仅是为了便于匹配处理排除一些干扰因素,没有别的
实际意义。

论坛徽章:
0
2 [报告]
发表于 2005-12-16 17:20 |只看该作者
ls|awk '{printf"mv %s %s \n",$0,tolower($0)}'|sh

论坛徽章:
0
3 [报告]
发表于 2005-12-20 10:07 |只看该作者
awk 命令比较好。精简而实用
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP