I am trying to rename .srt files. The files presently have names such as this:


I would like the resulting file to be :


After studying rename for awhile I came up with the command:

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

Which is supposed to save the first 19 characters and remove the rest then appending .srt to the first 19 characters. It's not working and I'm stumped. I have searched this out and can't find any help, as far as I can tell this should work. Any help would be greatly appreciated. Thanks

$ rename -n 's/^(.{18}).*$/$1.srt/' *.srt
StargateSG1.s02e19.One.False.Step.AC3.DVDRip.DivX-AMC.srt renamed as StargateSG1.s02e19.srt

You had 2 problems:

  1. \d stands for any digit, while you need . which is any character.
  2. You didn't match the rest of the filename, so the replacement operation wouldn't have removed it.
  • Can you please explain the .*$ and the ^ ? – Widgeteye Sep 1 '16 at 1:51
  • .* is any number of any characters. ^ is the start of input, $ is the end of input, their purpose is to ensure that we match the whole input from start to end, instead of just a portion of the input. – dezlov Sep 1 '16 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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