Rename multiple files with sed

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

Upvotes: 4

Views: 14823

Answers (4)

bartimar
bartimar

Reputation: 3534

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 :)

Upvotes: 2

Jose
Jose

Reputation: 123

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

Upvotes: 0

Endoro
Endoro

Reputation: 37569

GNU sed

for file in *.mkv; do mv "$file" "$(echo "$file" | sed -e 's/.*\(\S\+\)\s\+\S\+\s\(\S\+\)$/s\1e\2/')

Upvotes: 4

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

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

Upvotes: 9

Related Questions