Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash to batch rename files with adding numbers

I have a bunch of .jpg files with random names. I want a bash script to rename them like this:

basename-0.jpg
basename-1.jpg
basename-2.jpg
.
.
.
.
basename-1000.jpg

I wrote this:

n = 0;
for file in *.jpg ; do mv  "${file}" basename"${n}".jpg; n+=1;  done

But the problem with the above bash is that in the loop, n is considered as string so n+1 just adds another '1' to the end of newly moved file. Appreciate your hints.

like image 326
qliq Avatar asked Oct 19 '13 15:10

qliq


1 Answers

Use $((expression)) for arithmetic expansion in bash shell

n=0;
for file in *.jpg ; do mv  "${file}" basename"${n}".jpg; n=$((n+1));  done
like image 119
Yann Moisan Avatar answered Sep 30 '22 19:09

Yann Moisan