Reputation: 359
I have lots of file like these:
13831_1.jpg
13838_1.jpg
138035_1.jpg
138037_1.jpg
138039_1.jpg
I need to add a value of 5000000 to the numbers of the filenames. The result should be the following:
5013831_1.jpg
5013838_1.jpg
5138035_1.jpg
5138037_1.jpg
5138039_1.jpg
Is there a way to do it with bash or perl?
Upvotes: 9
Views: 10570
Reputation: 367
$filename = "13831_1.jpg";
$org = explode("_".$filename);
$addnumber = 5000000+$org[0];
$string = implode("_",$addnumber);
Upvotes: -3
Reputation: 8398
One way to do this using only bash
for file in *.jpg; do
number=${file%_*}
therest=${file#$number}
mv "$file" "$((number+5000000))$therest"
done
Notes:
*.jpg
will expand to a list of .jpg
files in current directory (ref: Filename expansion).${file%_*}
removes everything after _
in the file name and return it. (ref: Shell parameter expansion)${file#$number}
removes the content of variable number
from the beginning of the file name and return it. (ref: Shell parameter expansion)$((number+5000000))
evaluates the arithmetic expression inside and returns the result (ref: Arithmetic expansion)Upvotes: 6
Reputation:
Do: rename -v
. If it will output:
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
This check is because there are at least two different rename tools, with very different functionalities. And the solution I'm having requires rename that handles perlexpr.
Then you can:
rename 's/^(\d+)/5000000+$1/e' *.jpg
Upvotes: 9