我有一堆文件,例如:
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