【问题标题】:linux - rename command to remove all text after final dash before extension in filenamelinux - 重命名命令以删除文件名扩展名之前的最后一个破折号之后的所有文本
【发布时间】:2016-05-02 06:15:12
【问题描述】:

我有以下格式的文件:

word1 word2 word3-6b3kZ3q2i.mp4
word-8x2u3p6b4.mkv

其中一些有不止一个破折号。它们都有一个名称,后跟一个最后的破折号和一个字母数字 ID。

我想要做的是从当前目录中的每个文件名中删除最后的破折号及其后面的所有文本(文件扩展名除外)。

例如:word1 word2 word3-6b3kZ3q2i.mp4word1 word2 word3.mp4

我尝试过的类似问题的所有解决方案似乎都不起作用。 rename 每当我在字符串中放置一个破折号以查找或替换时就会起作用。

有什么想法吗?

【问题讨论】:

    标签: regex linux bash shell rename


    【解决方案1】:

    假设你有基于 Perl 的rename 命令,你可以使用这个rename 命令:

    rename -n 's/-[^.-]+(?=\.)//' *.*
    

    正则表达式分解:

    -       # match a hyphen
    [^.-]+  # match 1 or more of any char that is not hyphen or dot
    (?=\.)  # lookahead to assert that next char is dot
    

    【讨论】:

    • 我没有使用 perl 重命名。我正在使用 util-linux 的重命名。
    猜你喜欢
    • 1970-01-01
    • 2019-07-29
    • 2022-01-22
    • 2017-02-23
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2016-12-06
    相关资源
    最近更新 更多