gpt4 book ai didi

linux - Linux 下批量音乐目录重命名脚本/命令 (bash)

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:41 25 4
gpt4 key购买 nike

我有很多目录,其名称为:(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 来实现此目的:

重命名.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

关于linux - Linux 下批量音乐目录重命名脚本/命令 (bash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com