Max*_*huk 9 command-line files filename batch-rename
我有大约 180 个具有相同结构的文件:000-aaaaaaaa.txt.
文件名的正则表达式:/^[0-9]{3}\-[a-zA-Z]+$/gi(3 位数字 + -+ 字母 + .txt)。
我想剪切数字部分和-每个文件名
例如
000-hello.txt -> hello.txt001-world.txt -> world.txt002-ubuntu.txt -> ubuntu.txt003-linux.txt -> linux.txtbac*_*c0n 18
使用mmv重命名文件:
$ mmv '???-*' '#4'
^^^ ^ ^
123 4 4
Run Code Online (Sandbox Code Playgroud)
您还可以使用范围匹配来匹配数字:
$ mmv '[0-9][0-9][0-9]-*.txt' '#4.txt'
^ ^ ^ ^ ^
1 2 3 4 4
Run Code Online (Sandbox Code Playgroud)
(递归重命名文件):
$ mmv ';???-*' '#1#5'
^^^^ ^ ^ ^
1234 5 1 5
Run Code Online (Sandbox Code Playgroud)
; 扩展到任意数量的目录(与 相同**/)。* 匹配任何字符零次或多次。? 匹配任何单个字符。[] 匹配一个列表或一个字符范围。# 对 from 模式中第 n 个通配符字符的引用。ste*_*ver 13
使用基于 Perl 的rename命令:
$ rename -n 's/\d{3}-//' [0-9][0-9][0-9]-*.txt
rename(000-hello.txt, hello.txt)
rename(001-world.txt, world.txt)
rename(002-ubuntu.txt, ubuntu.txt)
rename(003-linux.txt, linux.txt)
Run Code Online (Sandbox Code Playgroud)
如果文件数量大到足以使命令超过 shell 的ARG_MAX,那么您可以使用
printf '%s\0' [0-9][0-9][0-9]-*.txt | xargs -0 rename -n 's/\d{3}-//'
Run Code Online (Sandbox Code Playgroud)
或者
find . -maxdepth 1 -name '[0-9][0-9][0-9]-*.txt' -exec rename -n 's/\d{3}-//' {} +
Run Code Online (Sandbox Code Playgroud)
请注意,[0-9][0-9][0-9]-*.txt由shell 处理并且需要是shell glob 表达式而不是正则表达式。
-n一旦你对它做正确的事情感到高兴,就删除它。
| 归档时间: |
|
| 查看次数: |
4450 次 |
| 最近记录: |