Sunday 15 December 2019

bash - How can I batch rename a set of filenames in Linux?


I have a folder with images named:


pic001-2.png
pic002-2.png
pic003-2.png

How do I rename them to the following?


pic001.png
pic002.png
pic003.png

I have tried mv "pic*-2.png" "pic*.png" but keep getting errors.



Answer



This will delete the first -2 found in each filename:


for f in pic*-2.png; do
mv "$f" "${f/-2/}"
done

To test it, just prepend echo to the mv line.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...