Reputation: 43
I am trying to change the name of a number of files in a directory using rename
, but the pattern that I'm using is not working with rename
, even though it works in my text editor (BBEdit). I would like to know how to modify the rename
command I'm using or the pattern so that I can get rid of the long prefix that each file has.
My directory listing looks like this:
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
I want to change this to
KIC Document 0001.pdf
Geo HW 2.pdf
kgs252_hw2.pdf
mids.png
Hw2.pdf
Using rename -vs 's/\D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_/''/g' *
produces no changes in the names. Nevertheless, changing the pattern \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_
to no character works just fine in my text editor. I've tried different things, e.g.
rename -vs \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_ '' *
and nothing.
mydir $ rename -nvs \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_ '' *
returns
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
Upvotes: 1
Views: 598
Reputation: 785481
Depending on your version of rename
, you may use this rename
command:
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'
If you are satisfied with the output remove -n
argument of dry-run.
Upvotes: 1