Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

to

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
add comment

2 Answers

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
add comment

Your regular expression should look like this:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.