我有很多目录,名称为:(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