Linux批处理音乐目录重命名脚本/命令(bash)

Linux批处理音乐目录重命名脚本/命令(bash),linux,bash,shell,batch-rename,Linux,Bash,Shell,Batch Rename,我有很多目录,它们的名字是:(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-2

我有很多目录,它们的名字是:(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 = "$2 $1";
    print "'$dir' -> '$new_name'\n";
    rename $dir, $new_name
    or die "Unable to rename '$dir' to '$new_name' : $!";
}
通常,如果坚持使用没有空格或特殊字符的文件/目录名,生活会变得更轻松。以下是如何使用它:

$ 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

谢谢你的帮助@Jens@Jens这是对的。请发布您迄今为止为解决此问题所做的工作。如果你想让我们帮忙,你应该先自己动手。