重命名命令删除子字符串

重命名命令删除子字符串

我正在尝试批量重命名以下文件:

art-faculty-3_29060055362_o.jpeg  
fine-arts-division-faculty-2016-2017-5_29165851925_o.jpeg  
theatre-faculty-2016-2017-1_29132529356_o.jpeg
art-history-faculty-2016-2017-1_29060057642_o.jpeg         
music-faculty-2016-2017-1_29132523816_o.jpeg

我想将它们重命名为:

art-faculty.jpeg                           
fine-arts-division-faculty.jpeg
theatre-faculty.jpeg
art-history-faculty.jpeg  
music-faculty.jpeg

这是我到目前为止所拥有的:

rename -n -D '/faculty(.*)/g' -X -v *

这将返回:

Using expression: sub { use feature ':5.18'; s/\/faculty\(\.\*\)\/g//g; s/\.   ([^.]+)\z//x and do { push @EXT, $1; $EXT = join ".", reverse @EXT } }
'art-faculty-3_29060055362_o.jpeg' unchanged
'art-history-faculty-2016-2017-1_29060057642_o.jpeg' unchanged
'fine-arts-division-faculty-2016-2017-5_29165851925_o.jpeg' unchanged
'music-faculty-2016-2017-1_29132523816_o.jpeg' unchanged
'theatre-faculty-2016-2017-1_29132529356_o.jpeg' unchanged

是否可以将 REGEX 与删除 (-D) 转换一起使用?如果是这样,我将如何使用它来进行上面显示的转换?如果没有,请指出rename使用 REGEX 执行转换的正确方向。

快速参考

答案1

for i in *.jpeg; do echo mv "$i" "${i%faculty*}faculty.jpeg" ; done

如果符合要求,删除echo以更改文件名


我的系统上的 perl rename 命令只有选项-v -f -n

$ rename -n 's/faculty\K.*(?=\.jpeg)//' *.jpeg
art-faculty-3_29060055362_o.jpeg renamed as art-faculty.jpeg
art-history-faculty-2016-2017-1_29060057642_o.jpeg renamed as art-history-faculty.jpeg
fine-arts-division-faculty-2016-2017-5_29165851925_o.jpeg renamed as fine-arts-division-faculty.jpeg
music-faculty-2016-2017-1_29132523816_o.jpeg renamed as music-faculty.jpeg
theatre-faculty-2016-2017-1_29132529356_o.jpeg renamed as theatre-faculty.jpeg

相关内容