批量音乐目录重命名 script/command 为 Linux (bash)

Batch music directory rename script/command for Linux (bash)

我有很多目录名称为:(YYYY) 艺术家 - 专辑

一些示例:

(2009) Vengaboys - Greatest Hits!
(2010) AC_DC - Iron Man 2
(2014) Simon & Garfunkel - The Complete Albums Collection
(2014) Various Artists - 100 Hits Acoustic
(2015) Various Artists - Graspop Metal Meeting 1996-2015

我怎样才能最好地将这些目录批量重命名为以下模板艺术家 - 专辑 (YYYY)?

因此上面的输出应该变成:

Vengaboys - Greatest Hits! (2009)
AC_DC - Iron Man 2 (2010)
Simon & Garfunkel - The Complete Albums Collection (2014)
Various Artists - 100 Hits Acoustic (2014)
Various Artists - Graspop Metal Meeting 1996-2015 (2015)

不应修改没有 (YYYY) 前缀的目录。

有人可以帮我用 linux bash 脚本或 sed 命令来实现吗?

使用 bash 处理空格和括号可能相当棘手。我会为此使用 perl:

rename.pl

#!/usr/bin/perl

use warnings;
use strict;

opendir my $dh, "./" or die "Unable to opendir : $!";
for my $dir ( readdir($dh) ) {
    # Skip anything that is not a directory matching '(YYYY) Name' 
    next unless -d "./$dir" and $dir =~ m|^(\(\d{4}\))\s(.*)$|;
    my $new_name = " ";
    print "'$dir' -> '$new_name'\n";
    rename $dir, $new_name
    or die "Unable to rename '$dir' to '$new_name' : $!";
}

通常,坚持使用 file/directory 个没有空格或特殊字符的名称会让生活变得更轻松。使用方法如下:

$ ls
(2009) Vengaboys - Greatest Hits!
(2010) AC_DC - Iron Man 2
(2014) Simon & Garfunkel - The Complete Albums Collection
(2014) Various Artists - 100 Hits Acoustic
(2015) Various Artists - Graspop Metal Meeting 1996-2015
rename.pl

$ perl rename.pl
'(2009) Vengaboys - Greatest Hits!' -> 'Vengaboys - Greatest Hits! (2009)'
'(2010) AC_DC - Iron Man 2' -> 'AC_DC - Iron Man 2 (2010)'
'(2014) Simon & Garfunkel - The Complete Albums Collection' -> 'Simon & Garfunkel - The Complete Albums Collection (2014)'
'(2014) Various Artists - 100 Hits Acoustic' -> 'Various Artists - 100 Hits Acoustic (2014)'
'(2015) Various Artists - Graspop Metal Meeting 1996-2015' -> 'Various Artists - Graspop Metal Meeting 1996-2015 (2015)'

$ ls
AC_DC - Iron Man 2 (2010)
Simon & Garfunkel - The Complete Albums Collection (2014)
Various Artists - 100 Hits Acoustic (2014)
Various Artists - Graspop Metal Meeting 1996-2015 (2015)
Vengaboys - Greatest Hits! (2009)
rename.pl