Bash Shell | Batch rename mutliple files and file extensions

In the last few months I often faced a task that becomes quite common to me. It is about to rename multiple files or more specific change their file extensions. Some context we run a Selenium based integration test and have to manage some images for the image comparison.  If the image changed because of an update we have to replace it with the newer  version. In some cases we have to replace a huge amount of images. That being the case if the  browser rendering is alternated or a font changes.

Our images are saved in the format *.ref.png for the reference images and *.test.png for the generated test images. To replace the images we simple have to change the file extension and copy/replace the existing files.

Native Batch / Shell script


The simplest approach is to use the console. The following batch script will replace the extension just fine. There is a multi-line and a one-line solution:
// multi line input solution
for file in *.test.png
do
mv "$file" "${file%.test.png}.ref.png"
done

// one line solution
for file in *.test.png; do mv "$file" "${file%.test.png}.ref.png"; done

 
{{ message }}

{{ 'Comments are closed.' | trans }}