Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I rename a file in linux and take the version # from the end and move it to the beginning. For example I have a file name:

Random text - text 1.1 (SD).mp3


1.1 Random text - text.mp3

Right now I can grab the version number but can't move it to the beginning, I have something like:

rename -n 's/(\d{1}\.\d{1}) \(SD\).mp3/$1.mp3/' *.mp3

which gives me

Random text - text 1.1.mp3

I tried to add a second expression group to match the beginning of the file so I can swap the beginning and the end but then nothing happens

share|improve this question
I see a space in the regex of your rename command. Is it supposed to be there, and if so should it be escaped with \? – DarenW Sep 12 '12 at 23:16
@darenw: Spaces do not need to be escaped in regexes. – grawity Sep 12 '12 at 23:17

Your regular expression should look like this:

rename -n 's/^(.*)(\d{1}\.\d{1})(.*)$/$2 $1$3/' *.mp3
share|improve this answer

The following should work:

's/^(.+?) (\d+\.\d+) \(SD\)/$2 $1/'

Or if you want to keep the (SD) part:

's/^(.+?) (\d+\.\d+) /$2 $1 /'
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .