shell脚本--根据用户提供的目录,将目录下大写的文件名改为小写

1.用for循环结合if判断,利用tr命令进行大小写转换:

#!/bin/bash
[ $# -eq 0 ] && echo "must be give a canshu" && exit      #判断用户是否输入了参数
[ ! -d $1 ] && echo "the $1 is not a directory" && exit     #判断用户输入的参数是否为目录
cd $1 || { echo "con't cd to the directory $1";exit 1; }        #切换到用户提供的目录
for filename in `ls`                                                          #遍历目录下的所有文件
do
   if [[ $filename != *[[:upper:]]* ]];then   #判断文件中是否包含大写字母,否则跳过进入下一循环,[[:upper:]]可以换成[A-Z]
      continue
   fi 
   new=`echo $filename | tr  'A-Z'  'a-z' `                    #将文件中的大写字母转换为小写
   mv $filename $new
   echo "the $filename is rename to $new"

done


脚本中稍作修改就可以将文件名是小写的文件,改为大写:

if [[ $filename != *[[:lower:]]* ]];then   #判断文件中是否包含小写字母,否则跳过进入下一循环,[[:lower:]]可以换成[a-z]
      continue
   fi 
   new=`echo $filename | tr  'a-z'  'A-Z' `     #将文件中的小写字母转换为大写

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值