I have a set of files (the list is larger, 4x43 files with extensions I.sto, Q.sto, U.sto and V.sto):
probni45069Q.sto probni45080I.sto probni45080V.sto probni45083U.sto
probni45069U.sto probni45080Q.sto probni45083I.sto probni45083V.sto
probni45069I.sto probni45069V.sto probni45080U.sto probni45083Q.sto
My goal is to rename them in the sorted order starting with number 1:
- probni45069I.sto probni1I.sto
- probni45080I.sto probni2I.sto
- probni45083I.sto probni3I.sto
- probni45069Q.sto probni1Q.sto
- probni45080Q.sto probni2Q.sto
- probni45083Q.sto probni3Q.sto
- probni45069U.sto probni1U.sto
- probni45080U.sto probni2U.sto
- probni45083U.sto probni3U.sto
- probni45069V.sto probni1V.sto
- probni45080V.sto probni2V.sto
- probni45083V.sto probni3V.sto
I have followed the instructions and guides from Renaming files in a folder to sequential numbers and created the following bash script:
model='probni'
for sto in I Q U V;
do
i=1
for j in $model*$sto.sto;
do
echo "$j" `printf $model%1d$sto.sto $i`
mv "$j" `printf $model%1d$sto.sto $i` 2>/dev/null || true
i=$((i + 1))
done
done
This script works great when used only once. The problem is when I use it multiple times on already sorted set of files or when I have a set of sorted files along with additional unsorted files (e.g. probni1I.sto + probni1346I.sto), I lose a certain number of files. When used repeatedly on my original 4x43 set of files, I finally end with the set of 4x7 files.
My questions is how to make this script to be idempotent for sorted files or how just to add new unsorted files to the sorted list without losing any files.
What about incrementing i in a loop as long as the destination file already exists?
Edit: I missed that your input and output files look the same. This would require you to either 1) change the naming scheme of either, 2) put them in different directories, or 3) start renaming no sooner than you encounter a gap in counting up from 1