【问题标题】:Rename the file names so that the appear in a specified sorted order重命名文件名,以便以指定的排序顺序显示
【发布时间】:2021-08-18 19:18:29
【问题描述】:

我有一个文件列表。它们目前按此顺序出现:

Advance Topics in Graphs _ Lec 11 _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Basics of Combinatorics _ Lec 12 _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Basics of Graph Theory _ Lec 6 _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm
Basics of Relations - 2 _ Lec 20 _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm
Basics of Relations _ Lec 19 _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Basics of Sets _ Lec 18 _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm

我想重命名文件,使文件名具有以下模式(为清楚起见,使用正则表达式表示)Lec\s[0-9]?[0-9]?文件名的中间,出现在文件名的开头。(这是排序顺序)。

最终结果将如下所示:

Lec 6 Basics of Graph Theory _  _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm
Lec 11 Advance Topics in Graphs _  _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Lec 12 Basics of Combinatorics _  _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Lec 18 Basics of Sets _  _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm
Lec 19 Basics of Relations _  _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Lec 20 Basics of Relations - 2 _  _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm

【问题讨论】:

  • 未来,您可以更改youtube-dl 的输出,以在播放列表或序列中添加前缀(如果您是这样观看视频的)!
  • @ti7 我总是这样做。但是我从我的朋友那里得到了这个内容。大声笑
  • 查看您是否有rename/ prename 可用。
  • @Shawn 找不到包(前名)。我将安装重命名。
  • @Shawn 我不知道 perl。能否请您框定表达式?

标签: linux shell command-line


【解决方案1】:

每当我遇到这样的问题时,我倾向于编写一个 1-off 脚本来在 Python 中完成工作,尽管你可以用任何选择的语言替换它(即 Perl、bash+awk、..)

这主要是为了确保您在真正进行重命名之前知道结果会是什么,这可能是一种破坏性操作(丢弃标识符,将所有输入文件重命名为具有相同名称,以便只有最后一个文件存在。. )!

import os
import re

def mapper(s):
    # adjust to taste, this just swaps the middle to the front
    prefix, index, postfix = re.match(r"^(.*) (?:Lec) (\d+) (.*)$", s).groups()
    return f"Lec {int(index):02} {prefix} {postfix}"  # pad index 2->02, etc.

mapping = {}  # start a new dictionary to store src->dest maps
for name_file in os.listdir():
    try:
        mapping[name_file] = mapper(name_file)
    except AttributeError:  # failed to match
        print(f"failed to match for {name_file}")

# display the change on separate lines
for src, dest in mapping.items():
    print(f"{src} -> {dest}")

# [return] or ^C to quit
input("continue?..")

# perform renames
for src, dest in mapping.items():
    os.rename(src, dest)

【讨论】:

  • 谢谢!这也可以是一个通用的答案。
【解决方案2】:

注意:这需要gawk,因为gensub

ls | awk '{a=gensub(/^(.*) (Lec [0-9]+) (.*)$/, "\\2 \\1\\3","1",$0);printf "mv \"%s\" \"%s\"\n",$0, a}' |  bash

【讨论】:

  • 文件存在于目录中。名称不存在于文本文件中。
  • 面临 gwak 的问题。感谢您的努力
  • 嘿。别客气。 gawk给你带来了什么麻烦?
  • E:无法定位包 gwak。 @tink
  • 哦。您使用的是哪个发行版?
【解决方案3】:

给定:

ls -1 *.webm
Advance Topics in Graphs _ Lec 11 _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Basics of Combinatorics _ Lec 12 _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Basics of Graph Theory _ Lec 6 _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm
Basics of Relations - 2 _ Lec 20 _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm
Basics of Relations _ Lec 19 _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Basics of Sets _ Lec 18 _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm 

我只想在 Ruby 中做一点小插曲:

ruby -e 'Dir.glob("*.webm"){
        |fn| File.rename(fn, fn.sub(/^([^_]*)_ ([^_]+? )_/,"\\2\\1_ _"))}'

然后:

ls -1 *.webm
Lec 11 Advance Topics in Graphs _ _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Lec 12 Basics of Combinatorics _ _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Lec 18 Basics of Sets _ _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm
Lec 19 Basics of Relations _ _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Lec 20 Basics of Relations - 2 _ _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm
Lec 6 Basics of Graph Theory _ _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm

但请注意,如果新名称与先前生成的名称相同,则此(以及此处的所有其他解决方案)将覆盖文件...

【讨论】:

    猜你喜欢
    • 2020-01-16
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2015-01-24
    • 2021-07-01
    相关资源
    最近更新 更多