我有很多文件想要重命名,手动重命名需要很长时间。它们是视频文件,通常采用以下格式:“节目名称 - 剧集编号 - 剧集名称”,例如“绝命毒师 - 101 - Pilot”。
我想做的是将“101”部分更改为我自己的约定“S01E01”。我认为在一系列节目中,该字符串的唯一连续部分是最后一个数字,即。 S01E01、S01E02、S01E03、S01E04 等...
任何人都可以给我关于如何在 Mac OS X 终端上执行此操作的建议。我认为使用 Automator 或其他批量重命名程序来完成此操作太复杂了...
Thanks
解决方案如下:
- 适用于 3 位数和 4 位数的季+剧集说明符(例如
107
对于第 1 季第 7 集,或1002
第 10 季第 2 集)
- demonstrates advanced
find
and bash
techniques, such as:
- the
-regex
主要通过正则表达式(而不是通配符模式,如-name
)
-
execdir
在与每个匹配文件相同的目录中执行命令(其中{}
包含匹配的文件name only)
- 调用临时的
bash
演示正则表达式匹配的脚本=~
并通过内置报告捕获组${BASH_REMATCH[@]}
多变的;命令替换($(...)
) 用零向左填充值;变量扩展以提取子字符串(${var:n[:m]}
).
# The regular expression for matching filenames (without paths) of interest:
# Note that the regex is partitioned into 3 capture groups
# (parenthesized subexpressions) that span the entire filename:
# - everything BEFORE the season+episode specifier
# - the season+episode specifier,
# - everything AFTER.
# The ^ and $ anchors are NOT included, because they're supplied below.
fnameRegex='(.+ - )([0-9]{3,4})( - .+)'
# Find all files of interest in the current directory's subtree (`.`)
# and rename them. Replace `.` with the directory of interest.
# As is, the command will simply ECHO the `mv` (rename) commands.
# To perform the actual renaming, remove the `echo`.
find -E . \
-type f -regex ".+/${fnameRegex}\$" \
-execdir bash -c \
'[[ "{}" =~ ^'"$fnameRegex"'$ ]]; se=$(printf "%04s" "${BASH_REMATCH[2]}");
echo mv -v "{}" "${BASH_REMATCH[1]}S${se:0:2}E${se:2}${BASH_REMATCH[3]}"' \;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)