4

How can i rename files with titles like Stargate SG-1 Season 01 Episode 01 to just "s01e01"? Variable numbering of course. I already have something like this:

for file in *.mkv; do mv "$file" "$(echo "$file" | sed -e "REGEX HERE")

I just need the sed command that does what i need.

Thanks

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • See related question http://stackoverflow.com/questions/10445934/change-multiple-files – Mihai8 Jun 16 '13 at 20:51
  • @user1929959, that question is about changing content of multiple files, this question asks about renaming multiple files. – doubleDown Jun 18 '13 at 00:09

4 Answers4

9

No need for sed, try this:

#!/bin/bash

for f in *.mkv;
do
    set -- $f
    mv "$f" s${4}e${6}
done

in action:

$ ls
Stargate SG-1 Season 01 Episode 01.mkv

$ ./l.sh 

$ ls
s01e01.mkv
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 1
    He asked for `sed`. How do you know that he doesn't need `sed`? This example may be only a single case, and he may have a lot of others with will need `sed`. Also, your answer is dependent on `bash` and he may be using another shell. – fiatjaf Dec 03 '14 at 14:09
  • well, OP accepted the answer so I guess it solved the issues. If you think you have a better solution, post it and see what happens :-) – Fredrik Pihl Dec 03 '14 at 14:13
  • 2
    Being the OP doesn't make him the final arbiter of the truth, and an answer being accepted does not mean it was good, in fact it doesn't mean it was even an answer. – fiatjaf Dec 03 '14 at 14:42
4

GNU sed

for file in *.mkv; do mv "$file" "$(echo "$file" | sed -e 's/.*\(\S\+\)\s\+\S\+\s\(\S\+\)$/s\1e\2/')
Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
2

Awk is also good for this

for file in *.mkv; do
   mv "$file" $(awk '{print "s", $4, "e", $6}' <<<$file).mkv
done

I think that this is not a problem for sed :)

bartimar
  • 3,374
  • 3
  • 30
  • 51
0

I would go this way to rename all *.mkv files:

ls *.mkv | awk '{print "mv \"" $0 "\" s" $4 "e" $6}' | sh

or

ls *.mkv | awk '{print "\"" $0 "\" s" $4 "e" $6}' | xargs mv

Jose
  • 123
  • 1
  • 12