Skip to content
Advertisement

Modification of file names

I have a list of more than 1000 files on the following format.

0521865417_roman_pottery_in_the_archaeological_record_2007.pdf
0521865476_power_politics_and_religion_in_timurid_iran_2007.pdf
0521865514_toward_a_theory_of_human_rights_religion_law_courts_2006.pdf
0521865522_i_was_wrong_the_meanings_of_apologies_2008.pdf

I am on Linux and want to change them as follows

2007_roman_pottery_in_the_archaeological_record.pdf
2007_power_politics_and_religion_in_timurid_iran.pdf
2006_toward_a_theory_of_human_rights_religion_law_courts.pdf
2008_i_was_wrong_the_meanings_of_apologies.pdf

Using rename and awk I managed to get

2007_roman_pottery_in_the_archaeological_record_2007.pdf
2007_power_politics_and_religion_in_timurid_iran_2007.pdf
2006_toward_a_theory_of_human_rights_religion_law_courts_2006.pdf
2008_i_was_wrong_the_meanings_of_apologies_2008.pdf

The remaining task is now to remove the last field that holds the year.

Advertisement

Answer

A solution that uses sed to generate the new names and the rename commands then pipes them to bash:

ls -1 | sed -r 's/[0-9]*_([A-Za-z_]*)_[a-z]{3}_([0-9]{4}).pdf$/mv & 2_1.pdf/g' | bash
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement