bash - Rename files based on symbols in the name -


i have lot of files (images) (file names consist numbers):

123456.jpg 369258.jpg 987123.jpg ... 

i need make copy of each in other folder (let's name output) , rename each of file based on numbers in name, (in pseudocode):

outputfilename = string(filename[0]) + string(filename[1]) + string(filename[2]+filename[3]) + ".jpg" 

so can see, renaming involves getting symbol in file name , getting sum of symbols in file name.

i need make script mass rename *.jpg in folder put script based on similar algorithm, , output renamed ones in output folder mentioned earlier.

this script should workable macos terminal , windows via cygwin shell.

i assume main problems are: how particular character of bash variable , how perform addition in bash.

  1. to obtain char bash variable can use form: ${var:start_index:length}.

  2. to perform addition: $((arg1 + arg2))

your resulting script may that:

#!/bin/bash  f in *.jpg   output=${f:0:1}${f:1:1}$((${f:2:1} + ${f:3:1})).jpg   mv -- "$f" "$output" done 

Comments