免费注册 查看新帖 |

Chinaunix

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

批量替换文件名中的空格与文件名改名 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-07 13:52 |只看该作者 |倒序浏览
find . -name "* *"|
while read name;do
        na=$(echo $name | tr ' ' '_')
        mv "$name" $na
done
tr命令:translation命令,用法:
tr [OPTION]... SET1 [SET2]
用set2来替换set1,将标准输入的字符串转为标准输出,如:
$tr a b
进入输入模式后,你所输入的所有的字母a都会被翻译成字母b,然后输出到屏幕上
$ tr a b
apple
bpple
aabbcc
bbbbcc
在上面的脚本中,tr ' ' '_',将echo通过管道传入的字符串中所有的空格翻译为下划线,然后赋值给na变量,再使用mv命令,也就完成了替换。
tr命令的其他用法,set1, set2可以使用一些特殊字符来匹配,如:
$ tr [:lower:] [:upper:] //将所有的小写字母翻译为大写
hello
HELLO
what
WHAT
其他特殊字符:
  [:alnum:]
         all letters and digits
  [:alpha:]
         all letters
  [:blank:]
         all horizontal whitespace
  [:cntrl:]
         all control characters
  [:digit:]
         all digits
  [:graph:]
         all printable characters, not including space
     [:lower:]
            all lower case letters
     [:print:]
            all printable characters, including space
     [:punct:]
            all punctuation characters
     [:space:]
            all horizontal or vertical whitespace
     [:upper:]
            all upper case letters
     [:xdigit:]
            all hexadecimal digits
     [=CHAR=]
            all characters which are equivalent to CHAR

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


#!/bin/sh
#
if [ $# -lt 1 ];
then
    echo "Usage:batch BaseName"
    exit
fi

for j in *.mp4
do
    mv "$j" $1_$j
done


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/11598/showart_1921157.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP