2

i have a number of files in a folder (on linux) called

picture20.mp4
picture21.mp4
picture100.mp4
picture115.mp4

e.t.c I would like to increase the number displayed in each name by 3 so that i have

picture23.mp4
picture24.mp4
picture103.mp4
picture118.mp4

I have experimented with the rename command, and tried to write a bash script using basename to extract the number but all of my tries didnt have the desired output. How can i do it?

filby
  • 378
  • 3
  • 11

1 Answers1

3

You can use:

for f in picture*.mp4; do
   n="${f/picture}"
   n="${n%.*}"; ((n+=3))
   echo mv "$f" "picture$n.mp4";
done
mv picture20.mp4 picture23.mp4
mv picture21.mp4 picture24.mp4

When you're satisfied just remove echo from above command.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    You might want to test if the file to be moved to doesn't already existing and/or loop through the files in reverse numerical order, e.g. `ls picture*mp4 | sed -E 's/picture([0-9]+).mp4/\1/' | sort -rn` or some such. – zerodiff Sep 18 '14 at 21:10
  • 1
    It did work for me but I had the issue detailed here: https://stackoverflow.com/questions/24777597/value-too-great-for-base-error-token-is-08 because the numbers I am using have preceding 0s. To resolve it I had to add an extra line inbetween `n="${n%.*}"` and `((n+=3))` of: `n=$((10#$n))` – Scott Anderson Jun 23 '19 at 21:52