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
  • 55,668
  • 25
  • 164
  • 183
Joe Cola
  • 343

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
  • 55,668
  • 25
  • 164
  • 183
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,101