【问题标题】:macos rename all files by keeping the last n charactersmacOS 通过保留最后 n 个字符来重命名所有文件
【发布时间】:2022-01-14 23:53:29
【问题描述】:

如何递归重命名

filename9_123.txt
filename10_124.txt
filename11_125.txt

123.txt
124.txt
125.txt

【问题讨论】:

    标签: macos command-line rename


    【解决方案1】:

    我可以使用红宝石吗?应该与默认安装的版本一起使用。注意:如果名称冲突,将覆盖文件。

     ruby -e "require 'pathname';Pathname.glob('*_*.txt')
        .each {|f| f.rename f.basename.to_s.split('_').last }"
    

    您可能需要删除换行符,这只是为了更好地格式化。

    如果“递归”是指在子目录中:

    ruby -e "require 'pathname';Pathname.glob('**/*_*.txt')
        .each {|f| f.rename(f.dirname / f.basename.to_s.split('_').last) }"
    

    此版本失败,它将覆盖现有文件:

     ruby -e "require 'pathname';Pathname.glob('**/*_*.txt')
        .to_h {|f| [f, f.dirname / f.basename.to_s.split('_').last] }
        .each {|from, to| fail(to) if to.exists?; from.rename to }"
    

    【讨论】:

    • 有没有办法防止覆盖名称冲突? @Matthias Winkelmann
    • 添加了支票@generic
    猜你喜欢
    • 2020-09-03
    • 2013-11-01
    • 2020-11-26
    • 2016-08-06
    • 2016-07-13
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多