Ubuntu – Capture groups are ignored when renaming files


I have a number of files in this format:

##.## - File name.mp4

I want to rename them to:


This is what I came up with:

rename -n "s/^(\d{2})\.(\d{2}).*/s$1e$2.mp4/"

It works when I tested it on http://regexr.com/ but when I run the command on my Ubuntu 12.04 installation all my files get renamed to se.mp4

So it looks like the groups are being ignored or not inserted or something along those lines. Am I missing something blindingly obvious or is something else going on?

I ended up using pyRenamer and that worked wonderfully, but I would still like to know why rename isn't working properly.

Cheers and thanks for any help you can give me.

Best Answer

I think perhaps your use of double quotes is allowing the shell to expand $1 and $2

$ rename -nv "s/^(\d{2})\.(\d{2}).*/s$1e$2.mp4/" *.mp4
12.34 File name.mp4 renamed as se.mp4

whereas with single quotes around the rename expression

$ rename -nv 's/^(\d{2})\.(\d{2}).*/s$1e$2.mp4/' *.mp4
12.34 File name.mp4 renamed as s12e34.mp4