
У меня есть куча файлов, например:
bla.super.lol.S01E03.omg.bbq.mp4
bla.super.lol.S01E04.omg.bbq.mp4
bla.super.lol.s03e12.omg.bbq.mp4
Мне нужно переименовать их все, например:
s01e03.mp4
s01e04.mp4
s03e12.mp4
Я пытался это сделать, for file in *; do mv $file ${file%%\.omg*}; done
но он удаляет только часть после S01E01, а не до него, поэтому, пожалуйста, помогите.
решение1
С rename
( prename
):
rename -n 's/^bla\.super\.lol\.[sS](\d+)[eE](\d+)\..*(\.mp4$)/s$1e$2$3/' *.mp4
-n
выполняет ли сухой прогон, если вы удовлетворены потенциальным переименованием, просто удалите, -n
чтобы произошло фактическое переименование:
rename 's/^bla\.super\.lol\.[sS](\d+)[eE](\d+)\..*(\.mp4$)/s$1e$2$3/' *.mp4
Пример:
$ 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
#!/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
решение3
Будь проще..
rename 's/.*\.(\w+)\.omg\..*mp4$/\L$1.mp4/' *.mp4
Пример:
$ echo 'bla.super.lol.S01E03.omg.bbq.mp4' | perl -pe 's/.*\.(\w+)\.omg\..*mp4$/\L$1.mp4/'
s01e03.mp4