我已经尝试了一堆重命名命令的例子,但我无法弄清楚做我想做的事的语法。
我有一堆文件标记为类似
File_Ex_1.jpg
File_Ex_2.jpg
File_Ex_3.jpg
File_Ex_4.jpg
...
File_Ex_10.jpg
File_Ex_11.jpg
etc.
Run Code Online (Sandbox Code Playgroud)
我只想通过插入 a 来更改其中的一些,0
以便文件名具有相同的字符数。
所以,我希望File_Ex_1.jpg
去File_Ex_01.jpg
,File_Ex_2.jpg
到File_Ex_02.jpg
您如何使用重命名命令执行此操作?
此rename
命令是通过Mac 上的home-brew安装的。的输出rename -v
:
rename -v
Usage:
rename [switches|transforms] [files]
Switches:
-0/--null (when reading from STDIN)
-f/--force or -i/--interactive (proceed or prompt when overwriting)
Wide character in print at /System/Library/Perl/5.18/Pod/Text.pm line 286.
-g/--glob (expand "*" etc. in filenames, useful in Windows™ CMD.EXE)
-k/--backwards/--reverse-order
-l/--symlink or -L/--hardlink
-M/--use=*Module*
-n/--just-print/--dry-run
-N/--counter-format
-p/--mkpath/--make-dirs
--stdin/--no-stdin
-t/--sort-time
-T/--transcode=*encoding*
-v/--verbose
Transforms, applied sequentially:
-a/--append=*str*
-A/--prepend=*str*
-c/--lower-case
-C/--upper-case
-d/--delete=*str*
-D/--delete-all=*str*
-e/--expr=*code*
-P/--pipe=*cmd*
-s/--subst *from* *to*
-S/--subst-all *from* *to*
-x/--remove-extension
-X/--keep-extension
-z/--sanitize
--camelcase --urlesc --nows --rews --noctrl --nometa --trim (see manual)
Run Code Online (Sandbox Code Playgroud)
Emi*_*aga 21
试试这个:
rename -e 's/\d+/sprintf("%02d",$&)/e' -- *.jpg
Run Code Online (Sandbox Code Playgroud)
例子:
$ ls
Device_Ex_10.jpg Device_Ex_1.jpg Device_Ex_4.jpg Device_Ex_7.jpg
Device_Ex_11.jpg Device_Ex_2.jpg Device_Ex_5.jpg Device_Ex_8.jpg
Device_Ex_12.jpg Device_Ex_3.jpg Device_Ex_6.jpg Device_Ex_9.jpg
$ rename -e 's/\d+/sprintf("%02d",$&)/e' -- *.jpg
$ ls
Device_Ex_01.jpg Device_Ex_04.jpg Device_Ex_07.jpg Device_Ex_10.jpg
Device_Ex_02.jpg Device_Ex_05.jpg Device_Ex_08.jpg Device_Ex_11.jpg
Device_Ex_03.jpg Device_Ex_06.jpg Device_Ex_09.jpg Device_Ex_12.jpg
Run Code Online (Sandbox Code Playgroud)
我从这里参考:https : //stackoverflow.com/questions/5417979/batch-rename-sequential-files-by-padding-with-zeroes
这里适合您的特定rename
实现。
使用您似乎正在使用的 rename 版本,以下表达式应该执行您请求的转换(示例包含名为 的文件的目录File_Ex_{1..11}.jpg
):
$ rename -n -e 's/_(\d\.)/_0$1/g' -- *.jpg
'File_Ex_1.jpg' would be renamed to 'File_Ex_01.jpg'
'File_Ex_2.jpg' would be renamed to 'File_Ex_02.jpg'
'File_Ex_3.jpg' would be renamed to 'File_Ex_03.jpg'
'File_Ex_4.jpg' would be renamed to 'File_Ex_04.jpg'
'File_Ex_5.jpg' would be renamed to 'File_Ex_05.jpg'
'File_Ex_6.jpg' would be renamed to 'File_Ex_06.jpg'
'File_Ex_7.jpg' would be renamed to 'File_Ex_07.jpg'
'File_Ex_8.jpg' would be renamed to 'File_Ex_08.jpg'
'File_Ex_9.jpg' would be renamed to 'File_Ex_09.jpg'
Run Code Online (Sandbox Code Playgroud)
(删除标志-n
以实际进行重命名。)
的参数-e
是 Perl 搜索和替换表达式;其中
_(\d\.)
匹配 am 下划线,后跟一个数字,然后是一个点,并将下划线替换为_0
,从而插入前导零。$1
是对括号内的组(数字和点)的反向引用,并在新文件名中保持不变。