Bash,需要更改多个目录中的词序

Bash, need to change word order in multiple directories

我有相当经典的 FLAC collection,其中每个专辑都是一个目录。我意识到我使用了 sub-optimal 结构并且需要重命名所有目录。

我目前的命名约定是:

作曲家(指挥)- 作品名称

例如

"Bach (Celibidache) - Mass in F minor"

我想将命名更改为

作曲家 - 作品名称(指挥)

"Bach - Mass in F minor (Celibidache)"

有一些可能的例外,(CONDUCTOR) 可能是 (CONDUCTOR, SOLOIST) 并且一些目录没有 (CONDUCTOR) 部分,应该保持原样。 NAME OF PIECE可以包含所有合法的字母和符号。

所有相册都位于同一个 parent 目录中,因此没有 sub-directories。

执行此操作的简单方法是什么?

使用 perl 重命名(一些发行版将其作为重命名 - Ubuntu 和相关的,一些作为前名 - Fedora 和 Redhat AFAIK)。先检查一下。

prename -n -- '-d && s/(\(.*\)) - (.*)/-  /' *
  • -n 不要重命名只打印结果 - 在您对结果满意后删除。
  • -- 选项结束,perlexpr 和文件开始
  • -d 检查文件是否为目录
  • s/.../.../ - 替换

示例:

[test01@localhost composers]$ ls -la
total 12
drwxrwxr-x  3 test01 test01 4096 Feb 14 12:37  .
drwxrwxr-x. 7 test01 test01 4096 Feb 14 12:23  ..
drwxrwxr-x  2 test01 test01 4096 Feb 14 12:37 'Bach (Celibidache) - Mass in F minor'
-rw-rw-r--  1 test01 test01    0 Feb 14 12:27 'Bach (Celibidache) - Mass in F minor.flac'
[test01@localhost composers]$ prename -n -- '-d && s/(\(.*\)) - (.*)/-  /' *
Bach (Celibidache) - Mass in F minor -> Bach - Mass in F minor (Celibidache)
[test01@localhost composers]$ prename -- '-d && s/(\(.*\)) - (.*)/-  /' *
[test01@localhost composers]$ ls -la
total 12
drwxrwxr-x  3 test01 test01 4096 Feb 14 12:38  .
drwxrwxr-x. 7 test01 test01 4096 Feb 14 12:23  ..
-rw-rw-r--  1 test01 test01    0 Feb 14 12:27 'Bach (Celibidache) - Mass in F minor.flac'
drwxrwxr-x  2 test01 test01 4096 Feb 14 12:37 'Bach - Mass in F minor (Celibidache)'

请注意,如果没有 -d,文件和目录都将被重命名。