3

I have a bunch of files like:

bla.super.lol.S01E03.omg.bbq.mp4
bla.super.lol.S01E04.omg.bbq.mp4
bla.super.lol.s03e12.omg.bbq.mp4

I need to rename them all like:

s01e03.mp4
s01e04.mp4
s03e12.mp4

I've tried to do it with for file in *; do mv $file ${file%%\.omg*}; done but it removes only part after S01E01, not before it so please, help

1
  • Is the number of sections always the same, or should we work with S0 something? And what is the distibguising marker: barbeque? mp4? (probably omg looking at your attempt). – Jacob Vlijm Apr 28 '16 at 18:47
9

With rename (prename):

rename -n 's/^bla\.super\.lol\.[sS](\d+)[eE](\d+)\..*(\.mp4$)/s$1e$2$3/' *.mp4

-n does the dry-running, if you are satisfied with the potential renaming, just remove -n to let the actual renamimg to take place:

rename 's/^bla\.super\.lol\.[sS](\d+)[eE](\d+)\..*(\.mp4$)/s$1e$2$3/' *.mp4

Example:

$ ls -1
bla.super.lol.S01E03.omg.bbq.mp4
bla.super.lol.S01E04.omg.bbq.mp4
bla.super.lol.s03e12.omg.bbq.mp4

$ rename -n 's/^bla\.super\.lol\.[sS](\d+)[eE](\d+)\..*(\.mp4$)/s$1e$2$3/' *.mp4
bla.super.lol.S01E03.omg.bbq.mp4 renamed as s01e03.mp4
bla.super.lol.S01E04.omg.bbq.mp4 renamed as s01e04.mp4
bla.super.lol.s03e12.omg.bbq.mp4 renamed as s03e12.mp4
2
  • You could also, if you know that s\d{2}e\d{2} will only show up once replace the bla\.super.... with just [A-Za-z0-9.]+ or even .+, since + is non-greedy. This would allow your answer to work for any input of the same pattern (garbage + s01e01 + garbage.mp4) – Mitch Apr 28 '16 at 19:08
  • @Musher FIle renaming should not be based on any assumption..also your regex could be simplified to just [\w.] ..also + is Greedy.. – heemayl Apr 28 '16 at 19:12
3
#!/bin/bash
IFS="\n"                               # Handle files with spaces in the names
for file in *.mp4; do
    newfile="${file/bla.super.lol./}"  # Strip the prefix you don't want
    newfile="${newfile/S/s}"           # Change the first S to an s
    newfile="${newfile/E/e}"           # Change the first E to an e
    newfile="${newfile.%omg.bbq*}"     # Strip the suffix you don't want
    newfile="${newfile}.mp4}"          # Tack on the file extension again
done
if [[ "$file" == "$newfile" ]]; then
    echo "Not renaming $file - no change decreed."
elif [[ -f "$newfile" ]]; then
    echo "Not renaming $file - $newfile already exists."
else
    mv -- "$file" "$newfile"           # Make the change
fi
1

Keep it simple..

rename 's/.*\.(\w+)\.omg\..*mp4$/\L$1.mp4/' *.mp4

Example:

$ echo 'bla.super.lol.S01E03.omg.bbq.mp4' | perl -pe 's/.*\.(\w+)\.omg\..*mp4$/\L$1.mp4/'
s01e03.mp4

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.