V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yanwen
V2EX  ›  问与答

求助,批量重命名不规则文件夹下面的文件后缀。

  •  
  •   yanwen · 2015-04-02 16:41:58 +08:00 · 2090 次点击
    这是一个创建于 3338 天前的主题,其中的信息可能已经有所发展或是发生改变。
    文件夹的列表是这样的:

    /2015.3.1/照片/惠州/P23017745.JPG
    /2015.3.1/照片/惠州/P23017735.JPG
    /2015.2.16/照片/23020018/P23017746.JPG
    /2015.2.16/照片/2315150018/P23017746.JPG




    想把后面的大写JPG 改为jpg

    不知道用awk怎么写呢?

    求帮忙。。谢谢大家。
    15 条回复    2015-04-03 15:33:58 +08:00
    Ansen
        1
    Ansen  
       2015-04-02 16:59:36 +08:00   ❤️ 1
    find /2015.3.1/照片/ -name "*.JPG" -exec rename .JPG .jpg {} \;
    yanwen
        2
    yanwen  
    OP
       2015-04-02 17:03:55 +08:00
    @Ansen 有没有递归的?
    9hills
        3
    9hills  
       2015-04-02 17:17:05 +08:00 via iPhone   ❤️ 1
    @yanwen find 本身就是递归的
    yanwen
        4
    yanwen  
    OP
       2015-04-02 18:18:18 +08:00
    @9hills
    @Ansen 执行了之后提示:

    find: rename: No such file or directory
    find: rename: No such file or directory
    find: rename: No such file or directory
    find: rename: No such file or directory
    find: rename: No such file or directory
    find: rename: No such file or directory
    find: rename: No such file or directory


    是什么原因呢??子目录太深了??还是??
    ooxxcc
        5
    ooxxcc  
       2015-04-02 18:27:03 +08:00   ❤️ 1
    不,你需要安装rename这个工具。。
    vincentxue
        6
    vincentxue  
       2015-04-02 18:32:43 +08:00   ❤️ 1
    for f in *.JPG; do mv -- "$f" "${f%.JPG}.jpg"; done;
    vincentxue
        7
    vincentxue  
       2015-04-02 18:59:31 +08:00   ❤️ 1
    不好意思 没看到你是有目录的,试试这样:

    find ./images -depth -name "*.JPG" | sed "s/\.JPG$//" | xargs -I% mv %.JPG %.jpg
    Ansen
        8
    Ansen  
       2015-04-03 08:51:36 +08:00   ❤️ 1
    @yanwen 检查一下 路径是否正确,相对||绝对 ,还有尽量不要用中文路径,
    比如 /2015.3.1/images/
    或者 :
    find /2015.3.1/ -name "*.JPG" -exec rename .JPG .jpg {} \;
    yanwen
        9
    yanwen  
    OP
       2015-04-03 10:09:49 +08:00
    @vincentxue 也会出现这样的错误。。 find: rename: No such file or directory
    @Ansen 有中文路径。。悲剧的。。
    vincentxue
        10
    vincentxue  
       2015-04-03 10:45:00 +08:00   ❤️ 1
    @yanwen 你什么系统,我在 OS X 10.10.2 (14C109) 测试通过,包括路径含有中文、英文、中英混合,有多层目录。
    vincentxue
        11
    vincentxue  
       2015-04-03 10:47:34 +08:00   ❤️ 1
    @yanwen 你要把 ./images 更换为你的照片的上层路径

    我是假设你的照片路径是这样的:

    my/images/2015.3.1/照片/惠州/P23017745.JPG
    my/images/2015.3.1/照片/惠州/P23017735.JPG
    my/images/2015.2.16/照片/23020018/P23017746.JPG
    my/images/2015.2.16/照片/2315150018/P23017746.JPG

    那你要 cd 到 my 这个目录,然后运行命令。
    vincentxue
        12
    vincentxue  
       2015-04-03 10:50:30 +08:00   ❤️ 1
    虽然这命令不怎么可能会出错,但运行之前建议备份一下,系统差异什么的,文件夹下文件丢失我不负责,Linux 还没学好。。
    yanwen
        13
    yanwen  
    OP
       2015-04-03 10:51:03 +08:00
    @vincentxue 我系统是10.9.5的。。不知道是不是系统有问题呢。。

    y4nwen-MacBook-Pro:图片 y4nwen$ find ./2015.4.1/ -depth -name "*.JPG" | sed "s/\.JPG$//" | xargs -I% mv %.JPG %.jpg
    mv: rename ./2015.4.1//431309换图/431309_010 ./2015.4.1//856 彩 明2015-4-1/432701/432701_000.JPG to ./2015.4.1//431309换图/431309_010 ./2015.4.1//856 彩 明2015-4-1/432701/432701_000.jpg: No such file or directory




    是空格的问题么??
    vincentxue
        14
    vincentxue  
       2015-04-03 11:52:35 +08:00   ❤️ 1
    应该是空格的问题,你这个输出也太难看了。。
    joyeblue
        15
    joyeblue  
       2015-04-03 15:33:58 +08:00   ❤️ 1
    @yanwen 有空格就要将路径放到引号中。

    find ./ -name "*.JPG" | sed -e 's/.JPG//g; s/\(\s\)/\\\1/g' | xargs -i% mv -v "%.JPG" "%".jpg
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4863 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:07 · PVG 15:07 · LAX 00:07 · JFK 03:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.