Ubuntu – How to Rename multiple files

rename

I have hundreds of images which I want to rename:

  • Current name A0619101. Rename to A1906101
  • Current name A0825223. Rename to A2508223

Actually the first and second digits must become third and fourth. And the third and fourth digits must become first and second.

Could you help me?

Best Answer

Assuming the current directory contains all files you wish to rename:

rename 's/^A(\d\d)(\d\d)/A$2$1/' *

Take care: this algorithm cannot swap file names, so if you have files A1122333 and A2211333, then afterwards the second will be gone and the first will still be called A1122333 (having be renamed twice).

The following script fixes that issue:

rename 's/^A(\d\d)(\d\d)/tmp-A$2$1/' *
rename 's/^tmp-//' *