1

how to remove a number available in a file name ?
In my example below as you can see i want to remove zero

$ rename -n 's/Season\ (\d{2})\ Episode\ (\d{2}).+(\D{2})/Season\ $1\ Episode\ $2.mp4/' *

Season 01 Episode 01.mp4 renamed as Season 01 Episode 01.mp44  
Season 01 Episode 02.mp4 renamed as Season 01 Episode 02.mp44  
Season 01 Episode 03.mp4 renamed as Season 01 Episode 03.mp44  
Season 01 Episode 04.mp4 renamed as Season 01 Episode 04.mp44  
Season 01 Episode 05.mp4 renamed as Season 01 Episode 05.mp44  
Season 01 Episode 06.mp4 renamed as Season 01 Episode 06.mp44  
Season 01 Episode 07.mp4 renamed as Season 01 Episode 07.mp44  
Season 01 Episode 08.mp4 renamed as Season 01 Episode 08.mp44  
Season 01 Episode 09.mp4 renamed as Season 01 Episode 09.mp44  
Season 01 Episode 10.mp4 renamed as Season 01 Episode 10.mp44  
Season 01 Episode 11.mp4 renamed as Season 01 Episode 11.mp44  
Season 01 Episode 12.mp4 renamed as Season 01 Episode 12.mp44  
Season 01 Episode 13.mp4 renamed as Season 01 Episode 13.mp44  
Season 01 Episode 14.mp4 renamed as Season 01 Episode 14.mp44  
Season 01 Episode 15.mp4 renamed as Season 01 Episode 15.mp44  
Season 01 Episode 16.mp4 renamed as Season 01 Episode 16.mp44  
Season 01 Episode 17.mp4 renamed as Season 01 Episode 17.mp44  
Season 01 Episode 18.mp4 renamed as Season 01 Episode 18.mp44  
Season 01 Episode 19.mp4 renamed as Season 01 Episode 19.mp44  
Season 01 Episode 20.mp4 renamed as Season 01 Episode 20.mp44  
Season 01 Episode 21.mp4 renamed as Season 01 Episode 21.mp44  
Season 01 Episode 22.mp4 renamed as Season 01 Episode 22.mp44  
Season 01 Episode 23.mp4 renamed as Season 01 Episode 23.mp44  
Season 01 Episode 25.mp4 renamed as Season 01 Episode 25.mp44  
Season 01 Episode 26.mp4 renamed as Season 01 Episode 26.mp44  
Season 02 Episode 00.mp4 renamed as Season 02 Episode 00.mp44  
Season 02 Episode (this zero needs to be removed)***0***15.mp4 renamed as Season 02 Episode 01.mp44  
Season 02 Episode ***0***16.mp4 renamed as Season 02 Episode 01.mp44  
Season 02 Episode ***0***17.mp4 renamed as Season 02 Episode 01.mp44  
Season 02 Episode ***0***18.mp4 renamed as Season 02 Episode 01.mp44  
Season 02 Episode ***0***19.mp4 renamed as Season 02 Episode 01.mp44  
Season 02 Episode 01.mp4 renamed as Season 02 Episode 01.mp44  
Season 02 Episode ***0***20.mp4 renamed as Season 02 Episode 02.mp44  
Season 02 Episode ***0***21.mp4 renamed as Season 02 Episode 02.mp44  
Season 02 Episode 02.mp4 renamed as Season 02 Episode 02.mp44  
Season 02 Episode 03.mp4 renamed as Season 02 Episode 03.mp44  
Season 02 Episode 04.mp4 renamed as Season 02 Episode 04.mp44  
Season 02 Episode 05.mp4 renamed as Season 02 Episode 05.mp44  
Season 02 Episode 06.mp4 renamed as Season 02 Episode 06.mp44  
Season 02 Episode 07.mp4 renamed as Season 02 Episode 07.mp44  
Season 02 Episode 08.mp4 renamed as Season 02 Episode 08.mp44  
Season 02 Episode 09.mp4 renamed as Season 02 Episode 09.mp44  
Season 02 Episode 10.mp4 renamed as Season 02 Episode 10.mp44  
Season 02 Episode 11.mp4 renamed as Season 02 Episode 11.mp44  
Season 02 Episode 12.mp4 renamed as Season 02 Episode 12.mp44  
Season 02 Episode 13.mp4 renamed as Season 02 Episode 13.mp44  
Season 02 Episode 14.mp4 renamed as Season 02 Episode 14.mp44  

And as you can see the extension is also not renamed properly, using the rename command. I also know that rename command is wrong so please provide the proper one.

  • rename -n 's/Season (\d{2}) Episode 0*(\d{2}).+(\D{2})/Season $1 Episode $2.mp4/', perhaps - just omit leading zeroes before two-digit numbers. – muru Nov 19 '16 at 9:17
  • Yeah that worked, the zeroes were removed, but the extension still is the same mp44 – devilz Nov 19 '16 at 9:20
  • Your last group is (\D{2}), which matches the mp before mp4, so the 4 is not captured by the regex. What do you want the group for? rename -n 's/Season (\d{2}) Episode 0*(\d{2}).+mp4/Season $1 Episode $2.mp4/' might be enough. – muru Nov 19 '16 at 9:23
  • Yeah it worked. Thanks a lot mate. Had bigger file names which got renamed to these smaller ones – devilz Nov 19 '16 at 9:32
2

The following simpler expression might be enough:

rename -n 's/Season (\d{2}) Episode 0*(\d{2}).+mp4/Season $1 Episode $2.mp4/'
  • Use a leading 0* with the second group to catch extra 0s before the two digit episode number.
  • The last group isn't needed.
  • You don't need to escape spaces.

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.