I've got numbered files (DSC_0001.jpg to DSC_0065.jpg), I know how to move them with mv DSC_{0001..0065}.jpg dir.

I have no idea how to move them and change the range of values.

mv DSC_{0001..0065}.jpg DSC_{0134..1198}.jpg is not working.

Is there another way to do this ?


Here is a script debugged and much improved thanks to Kamil Maciorowski:

for i in {0065..0001}; do
  n="$(printf '%04d' "$((10#$i + 133))")"
  mv DSC_$i.jpg DSC_$n.jpg


  • We're counting backwards ({0065..0001}, not {0001..0065}) because numbers are about to be increased. The problem wouldn't hit you in this particular case, but imagine your range was {0001..0150}. Processing forwards, the very first mv would overwrite the file number 0134. In this case it would be crucial to move files with higher numbers out of the way first, to prevent data loss. When moving to another (empty) directory (instead of just renaming files in a single directory), the problem doesn't exist.
  • 10#$i forces base-10 for this $i in shell arithmetic; without it a leading zero would trigger base-8, hence errors for numbers like 0008 which is not a valid octal number.
  • %04d format in printf is responsible for leading zeros in the output (e.g. 0134 instead of just 134). In this particular case n=0$((10#$i + 133)) would also work, only because our $((…)) yields a three-digit number each time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.