0

I have a question

I have the rename command, but after I run the rename starting from 00, how to start from 01, please help me

this is the command that I use

for i in *.mkv; do
  new=$(printf "Movie - %02d.mkv" "$a")
  mv -i -- "$i" "$new"
  let a=a+1
done
Ravexina
  • 54,268
  • 25
  • 157
  • 179
Joe Cola
  • 323
  • 4
  • 12
  • Such kind of renaming could be easily done by using the command `rename`, here is a nice example: [Renaming hundreds of files at once for proper sorting](https://askubuntu.com/a/473355/566421). – pa4080 Jul 15 '20 at 12:38

2 Answers2

2

Simple as adding a variable (a=1) at the beginning:

#!/bin/bash
a=1
for i in *.mkv; do
  new=$(printf "Movie - %02d.mkv" "$a")
  mv -i -- "$i" "$new"
  let a=a+1
done

Here is in one line as you asked for:

a=1; for i in *.mkv; do new=$(printf "Movie - %02d.mkv" "$a"); mv -i -- "$i" "$new"; let a=a+1; done
Ravexina
  • 54,268
  • 25
  • 157
  • 179
1

The same can be done with bash's arithmetic evaluation, too. And it's generally preferred over let builtin.

For exmaple,

#!/bin/bash

a=1
for i in *.mkv; do
  new=$(printf "Movie - %02d.mkv" "$a")
  mv -i -- "$i" "$new"
  ((a++))
done

On other hand, if you prefer a one-liner, rename command can do that:

rename 's/\d+/sprintf("Movie - %02d", $&)/e' *.mkv
P.P
  • 1,021
  • 8
  • 17