![对连续文件列表重新编号,同时保留文件名的其余部分](https://linux22.com/image/954947/%E5%AF%B9%E8%BF%9E%E7%BB%AD%E6%96%87%E4%BB%B6%E5%88%97%E8%A1%A8%E9%87%8D%E6%96%B0%E7%BC%96%E5%8F%B7%EF%BC%8C%E5%90%8C%E6%97%B6%E4%BF%9D%E7%95%99%E6%96%87%E4%BB%B6%E5%90%8D%E7%9A%84%E5%85%B6%E4%BD%99%E9%83%A8%E5%88%86.png)
我有一个系列不是按季编号的,而是按绝对顺序编号的,从 0 到 150 左右。我想按季重新编号这些文件,以便它们在 Plex 中被正确拾取。以下是“第 2 季”文件夹当前的样子(前 5 个文件):
'Show Name - 014 Episode Title 1 [480p] [x265].mkv'
'Show Name - 015 Episode Title 2 [480p] [x265].mkv'
'Show Name - 016 Episode Title 3 [480p] [x265].mkv'
'Show Name - 017 Episode Title 4 [480p] [x265].mkv'
'Show Name - 018 Episode Title 5 [480p] [x265].mkv'
等等。
如您所见,赛季从 14 开始编号,而不是 1。首先,我使用 Nautilus 的重命名工具将 替换为- 0
。S02E
现在它看起来是这样的:
anon@anonpc:~/Downloads/sandbox$ ls
'Show Name S02E14 Episode Title 1 [480p] [x265].mkv'
'Show Name S02E15 Episode Title 2 [480p] [x265].mkv'
'Show Name S02E16 Episode Title 3 [480p] [x265].mkv'
'Show Name S02E17 Episode Title 4 [480p] [x265].mkv'
'Show Name S02E18 Episode Title 5 [480p] [x265].mkv'
但我需要剧集编号从 01 开始。这是我能想到的最接近的:
for i in {14..18}; do mv Show\ Name\ S02E$i*.mkv Show\ Name\ S02E$((i-13))*; done
但这不能正常工作:
mv: target 'Show Name S02E18 Episode Title 5 [480p] [x265].mkv' is not a directory
现在看起来像这样:
anon@anonpc:~/Downloads/sandbox$ ls
'Show Name S02E14 Episode Title 1 [480p] [x265].mkv'
'Show Name S02E2*'
'Show Name S02E3*'
'Show Name S02E4*'
'Show Name S02E5*'
您可以看到,第一个通配符*
可以获取完整的文件名,但在对文件进行重新编号时,其余通配符则不适用mv
。另外,我不知道为什么列表中的第一个项目没有被重命名。
我想知道如何按照上述方式重新编号文件,同时保留整数两侧的原始文件名。我使用的是带有默认 bash shell 的 Ubuntu 22.04。谢谢
答案1
Bash 通配符仅适用于现有文件。在这种情况下,您只需使用 Bash 前缀删除 ( ${variable#prefix_to_remove}
) 即可指定所需的重命名目标:
prefix=Show\ Name\ S02E
for i in {14..18}; do
x=("$prefix"$i*.mkv) # Use glob to find the original file with the title
y="${x#*S02}" # Remove the constant show name part
y="${y#* }" # Remove the variably unknown part by removing up to the next space
mv "$x" "$prefix$((i-13)) $y" # Compose the destination
done
测试运行:
home@daniel-tablet1:~/tmp$ ll
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E14 Episode Title 1 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E15 Episode Title 2 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E16 Episode Title 3 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E17 Episode Title 4 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E18 Episode Title 5 [480p] [x265].mkv'
home@daniel-tablet1:~/tmp$ prefix=Show\ Name\ S02E; for i in {14..18}; do x=("$prefix"$i*.mkv); y="${x#*S02}"; y="${y#* }"; mv "$x" "$prefix$((i-13)) $y"; done
home@daniel-tablet1:~/tmp$ ll
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E1 Episode Title 1 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E2 Episode Title 2 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E3 Episode Title 3 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E4 Episode Title 4 [480p] [x265].mkv'
.rw-rw-r-- 0 home 25 Jan 18:57 'Show Name S02E5 Episode Title 5 [480p] [x265].mkv'
答案2
sort
您可以在 GNU或 GNU中使用版本控制排序选项,ls
如下所示rename
:
printf '%s\n' * | sort -V | rename -v -n 'our $i; ++$i; s/([^\d]+)([\d]+)([^\d]+)([\d]+)([^\d]+)([\d]+)(.*\.mkv)/$1$2$3$i$5$6$7/'
... 或者:
ls -v | rename -v -n 'our $i; ++$i; s/([^\d]+)([\d]+)([^\d]+)([\d]+)([^\d]+)([\d]+)(.*\.mkv$)/$1$2$3$i$5$6$7/'
([^\d]+)
是一个用于非数字的捕获组,并且([\d]+)
仅用于数字,并且$1
,,$2
...$3
等是它们的参考编号......您可能需要根据文件名中的字符模式进行调整。
注意如果文件名中包含换行符,这些将会失败。
该-n
选项rename
用于空运行模式(文件系统上没有发生实际的文件重命名)...所以,尝试这些,如果您同意输出,则再次重新运行,但不使用像此演示这样的-n
选项:rename
$ ls -v | rename -v -n 'our $i; ++$i; s/([^\d]+)([\d]+)([^\d]+)([\d]+)([^\d]+)([\d]+)(.*\.mkv)/$1$2$3$i$5$6$7/'
Reading filenames from file handle (GLOB(0x559cb54612b0))
rename(Show Name S02E9 Episode Title 1 [480p] [x265].mkv, Show Name S02E1 Episode Title 1 [480p] [x265].mkv)
rename(Show Name S02E10 Episode Title 2 [480p] [x265].mkv, Show Name S02E2 Episode Title 2 [480p] [x265].mkv)
rename(Show Name S02E11 Episode Title 3 [480p] [x265].mkv, Show Name S02E3 Episode Title 3 [480p] [x265].mkv)
rename(Show Name S02E12 Episode Title 4 [480p] [x265].mkv, Show Name S02E4 Episode Title 4 [480p] [x265].mkv)
rename(Show Name S02E16 Episode Title 5 [480p] [x265].mkv, Show Name S02E5 Episode Title 5 [480p] [x265].mkv)
rename(Show Name S02E17 Episode Title 6 [480p] [x265].mkv, Show Name S02E6 Episode Title 6 [480p] [x265].mkv)
rename(Show Name S02E20 Episode Title 7 [480p] [x265].mkv, Show Name S02E7 Episode Title 7 [480p] [x265].mkv)
rename(Show Name S02E21 Episode Title 8 [480p] [x265].mkv, Show Name S02E8 Episode Title 8 [480p] [x265].mkv)
rename(Show Name S02E50 Episode Title 9 [480p] [x265].mkv, Show Name S02E9 Episode Title 9 [480p] [x265].mkv)
rename(Show Name S02E100 Episode Title 10 [480p] [x265].mkv, Show Name S02E10 Episode Title 10 [480p] [x265].mkv)
$
$
$ ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:12 'Show Name S02E100 Episode Title 10 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E10 Episode Title 2 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E11 Episode Title 3 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E12 Episode Title 4 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E16 Episode Title 5 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:11 'Show Name S02E17 Episode Title 6 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:11 'Show Name S02E20 Episode Title 7 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:11 'Show Name S02E21 Episode Title 8 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:12 'Show Name S02E50 Episode Title 9 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E9 Episode Title 1 [480p] [x265].mkv'
$
$
$ ls -v | rename -v 'our $i; ++$i; s/([^\d]+)([\d]+)([^\d]+)([\d]+)([^\d]+)([\d]+)(.*\.mkv$)/$1$2$3$i$5$6$7/'
Reading filenames from file handle (GLOB(0x55d02a78c0a0))
Show Name S02E9 Episode Title 1 [480p] [x265].mkv renamed as Show Name S02E1 Episode Title 1 [480p] [x265].mkv
Show Name S02E10 Episode Title 2 [480p] [x265].mkv renamed as Show Name S02E2 Episode Title 2 [480p] [x265].mkv
Show Name S02E11 Episode Title 3 [480p] [x265].mkv renamed as Show Name S02E3 Episode Title 3 [480p] [x265].mkv
Show Name S02E12 Episode Title 4 [480p] [x265].mkv renamed as Show Name S02E4 Episode Title 4 [480p] [x265].mkv
Show Name S02E16 Episode Title 5 [480p] [x265].mkv renamed as Show Name S02E5 Episode Title 5 [480p] [x265].mkv
Show Name S02E17 Episode Title 6 [480p] [x265].mkv renamed as Show Name S02E6 Episode Title 6 [480p] [x265].mkv
Show Name S02E20 Episode Title 7 [480p] [x265].mkv renamed as Show Name S02E7 Episode Title 7 [480p] [x265].mkv
Show Name S02E21 Episode Title 8 [480p] [x265].mkv renamed as Show Name S02E8 Episode Title 8 [480p] [x265].mkv
Show Name S02E50 Episode Title 9 [480p] [x265].mkv renamed as Show Name S02E9 Episode Title 9 [480p] [x265].mkv
Show Name S02E100 Episode Title 10 [480p] [x265].mkv renamed as Show Name S02E10 Episode Title 10 [480p] [x265].mkv
$
$
$ ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:12 'Show Name S02E10 Episode Title 10 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E1 Episode Title 1 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E2 Episode Title 2 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E3 Episode Title 3 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E4 Episode Title 4 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:10 'Show Name S02E5 Episode Title 5 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:11 'Show Name S02E6 Episode Title 6 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:11 'Show Name S02E7 Episode Title 7 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:11 'Show Name S02E8 Episode Title 8 [480p] [x265].mkv'
-rw-rw-r-- 1 ubuntu ubuntu 0 يناير 26 10:12 'Show Name S02E9 Episode Title 9 [480p] [x265].mkv'
这样做的好处是可以自动处理任何任意/变化的编号序列,并仍然根据1
其原始排序状态将其重命名为序列号。