我正在尝试使用rename
更改目录中的多个文件的名称,但我使用的模式不能与rename
一起使用,即使它在我的文本编辑器(BBEdit)。我想知道如何修改我使用的rename
命令或模式,以便我可以摆脱每个文件的长前缀。
我的目录列表如下所示:
bos012_attempt_2018-02-15-01-52-18_KIC Document 0001.pdf
gem512_attempt_2018-02-14-20-30-11_Geo HW 2.pdf
kgs252_attempt_2018-02-14-23-35-03_kgs252_hw2.pdf
nrs728_attempt_2018-02-15-10-04-42_mids.png
oko018_attempt_2018-02-15-23-57-57_Hw2.pdf
我想将此更改为
KIC Document 0001.pdf
Geo HW 2.pdf
kgs252_hw2.pdf
mids.png
Hw2.pdf
使用rename -vs 's/\D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_/''/g' *
不会对名称进行任何更改。尽管如此,将模式\D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_
更改为无字符在我的文本编辑器中工作正常。我尝试过不同的事情,例如
rename -vs \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_ '' *
mydir $ rename -nvs \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_ '' *
返回
Using expression: sub { use feature ':5.18';
s/\Q${\"DDDddd_attempt_2018\-dd\-dd\-dd\-dd\-dd_"}// }
'bos012_attempt_2018-02-15-01-52-18_KIC_Document_0001.pdf' unchanged
'gem512_attempt_2018-02-14-20-30-11_Geo_HW_2.pdf' unchanged
'nrs728_attempt_2018-02-15-10-04-42_mids.png' unchanged
'oko018_attempt_2018-02-15-23-57-57_Hw2.pdf' unchanged
答案 0 :(得分:1)
根据您的rename
版本,您可以使用此rename
命令:
rename -n 's/.*_attempt_\d{4}(-\d{2}){5}_//' *.{pdf,png}
'bos012_attempt_2018-02-15-01-52-18_KIC Document 0001.pdf' would be renamed to 'KIC Document 0001.pdf'
'gem512_attempt_2018-02-14-20-30-11_Geo HW 2.pdf' would be renamed to 'Geo HW 2.pdf'
'kgs252_attempt_2018-02-14-23-35-03_kgs252_hw2.pdf' would be renamed to 'kgs252_hw2.pdf'
'oko018_attempt_2018-02-15-23-57-57_Hw2.pdf' would be renamed to 'Hw2.pdf'
'nrs728_attempt_2018-02-15-10-04-42_mids.png' would be renamed to 'mids.png'
如果您对输出删除干运行的-n
参数感到满意。