Onliner Batch Rename Files with Bash

Because I’m fed of searching for this kind of useful command :

$ for F in *; do E=${F:(-4)}; B=${F/$E/}; mv $F 'Wonderful.Serial.FileName.S03E'$B.'VOSTFR.HDTV.XViD.GKS'$E; done

Explanations :
E = ${F:(-4)} extracts the file extension.

B = ${F/$E/} returns the original filename without extension.

These two variables are then used to build the new filename 🙂


Another one, using sed and regular expressions :

$ for A in *.avi; do mv "$A" `echo $A | sed -r 's/^Name To Remove <ins>([0-9]</ins>(-[0-9]+)?)\.avi$/\1.avi/'`; done

Were we build a simple “mv” command with the original filename ($A) and a new filename by catching the output of echo $A | sed enclosing it with backquotes (`).

sed -r allows to use extended regular expressions.

s/you/me/ is the sed command to Subsitute you by me.

s/^Name To Remove ([0-9](-[0-9]+)?)\.avi$/\1.avi/

We match filenames like :

  • Name To Remove 123.avi (as many spaces as you want between the textual part of the name and the file number)
  • Name To Remove 123-456.avi

    We catch the subexpression between parenthesis (123 or 123-456 in this poor example) and we use it to build the new filename (thanks to ”\1” in the third part of the sed command.)

    Be Sociable, Share!

Tags: , , ,


Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *