1

I'm on OSX 10.9.2 (BASH 3.5) and I wanted to rename a whole folder content names. So I run a script and somehow ended with this:

s01eDavid.Letterman.2014.02.27.Stupid.Human.Tricks.x264-SWOLLED.mp4
s01eDavid.Letterman.2014.03.07.Lenny.Marcus.HDTV.x264-BATV.mp4
s01eDavid.Letterman.2014.03.24.Jimmy.Carter.HDTV.x264-SWOLLED.mp4
s01eDavid.Letterman.2014.03.25.Emma.Watson.720p.HDTV.x264-SWOLLED.mkv

(There is extra S01e at the beggining of each sentence while there was supposed to be the S01e01 , S01e02.....)

How can I remove/rename those files to make it look like that?:

s01e1 David.Letterman.2014.02.27.Stupid.Human.Tricks.x264-SWOLLED.mp4
s01e2 David.Letterman.2014.03.07.Lenny.Marcus.HDTV.x264-BATV.mp4
s01e3 David.Letterman.2014.03.24.Jimmy.Carter.HDTV.x264-SWOLLED.mp4
s01e4 David.Letterman.2014.03.25.Emma.Watson.720p.HDTV.x264-SWOLLED.mkv

Thanks!

1

Not an awk solution:

cnt=1
for f in *; do 
  fcnt=$(printf "%02d" $cnt)
  echo mv "$f" "$(echo $f | sed -e "s/^s01e/&$fcnt /")"
  cnt=$(($cnt+1)); 
done

I made the episode number have 2 digits using the printf command.

This just prints the commands to stdout, so you can check whether you like the output. Remove the leading "echo" from line 4 when you want to rename for real.

  • Thanks Daniel! however... i get this message:/Users/rshloosh/unix_files/numbering.sh: line 9: 08: value too great for base (error token is "08") and only the first 8 lines are bing processed.. lines 9 and 10 ( last are not..) this is how the folder looks like: – user67486 May 13 '14 at 11:40
  • david.letterman.2014.04.24.neil.patrick.harris.720p.hdtv.x264-bajskorv.mkv s01e08 08 david.letterman.2014.04.25.michael.j.fox.720p.hdtv.x264-bajskorv.mkv s01edavid.letterman.2014.04.30.kiefer.sutherland.720p.hdtv.x264-bajskorv.mkv s01edavid.letterman.2014.05.01.louis.c.k.720p.hdtv.x264-bajskorv.mkv – user67486 May 13 '14 at 11:44
  • @user67486 Your error message should be gone now.. And I thought you had s01e in front of every file name; how do you expect other file names to be handled? – daniel kullmann May 13 '14 at 12:52
  • **i had s01e in front of every line ... after i run the script twice that was he result... iwould like to have s01eXX where xx incremnet in the start of lines... ** – user67486 May 13 '14 at 13:04
0
episode=1
for x in s01e*; do
  mv "$x" "s01e$episode ${x#s01e}"
  episode=$((episode+1))
done

If there are 10 or more episodes, you may prefer to have a leading 0 for numbers up to 9. An easy trick for that is to generate numbers in the range 100–199 and trim the leading 1 digit.

episode=101
for x in s01e*; do
  mv "$x" "s01e${episode#1} ${x#s01e}"
  episode=$((episode+1))
done

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.