Bash

批量改名電視劇

  • August 9, 2017

我的 iTunes 庫位於 NAS(WD MyCloud 4TB)上,並且我有許多電視連續劇,通常由 iTunes 庫組織:

“電視節目”文件夾:

TV Show 1

|------------- Series 1
              |-----------01 Episode Name.m4v
              |-----------02 Episode Name.m4v
              |...
|------------- Series 2
              |-----------01 Episode Name.m4v
              |...
|...
TV Show 2
|------------- Series 1
              |-----------01 Episode Name.m4v
              |-----------02 Episode Name.m4v
              |...
|------------- Series 2
              |-----------01 Episode Name.m4v
              |...
|...

我已將整個 TV 文件夾硬連結到另一個名為 Infuse 的文件夾。我的 Apple TV 上的 Infuse 應用程序將讀取此文件夾

$$ this allows me to bypass having a computer with iTunes permanently switched on $$,所以我需要像這樣重命名所有硬連結文件:

TV Show 1
|------------- Series 1
              |-----------TV Show 1 - S0101 Episode Name.m4v
              |-----------TV Show 1 - S0102 Episode Name.m4v
              |...
|------------- Series 2
              |-----------TV Show 1 - S0201 Episode Name.m4v
              |...
|...
TV Show 2
|------------- Series 1
              |-----------TV Show 2 - S0101 Episode Name.m4v
              |-----------TV Show 2 - S0102 Episode Name.m4v
              |...
|------------- Series 2
              |-----------TV Show 2 - S0201 Episode Name.m4v
              |...
|...

到目前為止,我已經找到了解決方案(例如 TV Show 1, Season 1 文件夾):

cd進入每個節目的每個季節文件夾並執行

for f in *; do mv $f "TV Show 1 E01S$f"; done

但這真的很耗時,因為我需要

cd ../Season 2/
for f in *; do mv $f "TV Show 1 E02S$f"; done
cd ../Sesaon 3/

...

cd ../../TV Show 2/Season 1/
for f in *; do mv $f "TV Show 2 E01S$f"; done
...

我最理想的情況是編寫腳本從祖父目錄中提取名稱和從父目錄中提取季節編號,這樣我就可以編寫一個簡短的 bash 腳本並執行它。就像是:

#!/bin/bash
a=[TV Show folder]
b=[Season folder]
c=[each episode]

for c in each a/b/
mv $c "$a - S0$bE$c"    (or)   rename $c "$a - S0$bE$c"
done

你能幫我用一些 for 循環或特定命令來實現它嗎?

最近做了一些非常相似的事情,我已經有了一個腳本。

for f in */*/*
do
 destdir=${f%/*}
 tvshow=${f%%/*}
 season=${destdir#*/}
 episode=${f##*/}
 # Get season number
 seasonnum=${season##* }

 dest=$(printf "%s/%s - S%02iE%s" "$destdir" "$tvshow" $seasonnum "$episode")
 echo "mv -- \"$f\" \"$dest\""
 # mv -- "$f" "$dest"
done

zsh

autoload zmv # best in ~/.zshrc
zmv -n '(*)/(Series (<->))/(<->)(*)' '$1/$2/$1 - S${(l:2::0:)3}E${(l:2::0:)4}$5'

-n(高興時刪除)。

  • <->用於<x-y>數字xy但既不提供x也不y提供,因此任何(十進制)數字。
  • ${(l:2::0:)var}: 寬度為 2 的左焊盤,帶0s

perl‘s相同rename

rename -n 's{./(.*)/Series (\d+)/\K\d+}{
            sprintf "%s - S%02dE%02d", $1, $2, $&}se' ./*/*/*

引用自:https://unix.stackexchange.com/questions/384889