我有一个 38GB 的文件夹,里面有 800 个 MP4 视频。重新下载后,文件名没有空格,所有单词都连在一起,但还是TitleCase。
所以从TitleCase我需要Title Case。
批量重命名这些文件的最有效方法是什么?
我记得很久以前我的发行版中就包含了rename或autorename,但现在似乎没有了。
Gil*_*not 12
如果要在 mp4 文件名 TitleCase 的每个“单词”之间添加空格(PascalCase 到以空格分隔的单词):
rename -n 's/\B[[:upper:]]/ $&/g' ./*.mp4
Run Code Online (Sandbox Code Playgroud)
rename(./FooBarBaz.mp4, ./Foo Bar Baz.mp4)
Run Code Online (Sandbox Code Playgroud)
rename重命名不同的版本和用法怎么样?特别推荐使用 Perl 版本的方式是什么?
我不知道什么是最有效的方法(我认为你的意思是有效),但我会快速编写一个 for 循环,例如:
\nfor file in *.mp4; do\n newname="$(echo "$file" | sed \'s/\\(.\\)\\([A-Z]\\)/\\1 \\2/g\')"\n mv "${file}" "${newname}"\ndone\nRun Code Online (Sandbox Code Playgroud)\n解释:
\n newname="$(echo "$file" | sed \'s/\\(.\\)\\([A-Z]\\)/\\1 \\2/g\')"\n# ^-------------------- Assign to variable "newname" value\xe2\x80\xa6\n# ^------------------- "$()": as output by commannd in parentheses;\n# use "" to avoid word splitting\nRun Code Online (Sandbox Code Playgroud)\n在哪里
\necho $file | sed \'s/\\(.\\)\\([A-Z]\\)/\\1 \\2/g\'\n# ^-------------------------------------- output old file name\n# ^-------------------------------- pipe to `sed` command\nRun Code Online (Sandbox Code Playgroud)\nsed是“流编辑器”的名称;它接受输入,对其执行命令,并产生输出。这里的命令是s,如“搜索和替换”中所示。
s/\\(.\\)\\([A-Z]\\)/\\1 \\2/g\n^^ ^ ^ ^ ^ ^ ^ ^\n|| |^ | | ^^^ | | | |\n\\------------------------ s: search and replace\n \\----------------------- /: Set the search;replace;flags separator to "/"\n || | | \\|/ | \\ / |\n \\--+------------------ \\(\xe2\x80\xa6\\): a "capture group" (the first one);\n | | | | || | whatever is matches the content will be\n | | | | \\| | available as \\1\n \\-------------------- .: We match ".", which means *any* character\n | | | | | (which precludes this from matching at start of line)\n \\------+--------- \\(\xe2\x80\xa6\\): Second capture group, \\2\n \\------------- [A-Z]: Match any capital letter\n | |\n \\----- Replacement: "\\1 \\2" replace\n | "characterbeforecapitalletter""Capitalletter" with\n | "characterbeforecapitalletter" "Capitalletter"\n |\n \\- g: Flag that means "global": Repeat this until\n end of line\nRun Code Online (Sandbox Code Playgroud)\n