Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename multiple files with sed

Tags:

linux

rename

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

like image 486
Gonçalo Silva Santos Avatar asked Dec 06 '22 07:12

Gonçalo Silva Santos


2 Answers

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
like image 167
Fredrik Pihl Avatar answered Dec 19 '22 07:12

Fredrik Pihl


GNU sed

for file in *.mkv; do mv "$file" "$(echo "$file" | sed -e 's/.*\(\S\+\)\s\+\S\+\s\(\S\+\)$/s\1e\2/')
like image 32
Endoro Avatar answered Dec 19 '22 08:12

Endoro