Squan Schmaan
Squan Schmaan

Reputation: 385

Batch rename files with iteration

I have an experiment with 60 plots. I took 5 photos per treatment with a go-pro, plus a 6th photo of the sky to mark the point where I moved to the next plot (Total 360 photos). How could I write a bash script to rename these files automatically; i.e

Change the set of files:

GOPRO0001.jpg, GOPRO0002.jpg, ... , ... , GOPRO0360.jpg

Into something like:

plot1_1.jpg, ... , plot1_6.jpg, ..., ..., plot60_1.jpg, ... , plot60_6.jpg

What's the most efficient way of doing this? My thinking is I need to have 2 levels of iteration, but I'm not sure how to do it..

Upvotes: 0

Views: 169

Answers (1)

Quasímodo
Quasímodo

Reputation: 4004

You can try this BASH script:

#!/bin/bash
i=1;j=1
for file in GOPRO*.jpg; do
    mv "$file" "plot${j}_${i}.jpg"
    if [[ $i -eq 6 ]]; then
        i=1
        ((j++))
    else
        ((i++))
    fi
done

If you want to verify what it would do before actually letting it take action, put an echo before the mv command.

Upvotes: 1

Related Questions