我有很多目录名称:(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命令来实现这一目标吗?
答案 0 :(得分:0)
使用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