我在一个目录中有以下文件:
1-1 - different text for each file here.txt
1-2 - different text for each file here.txt
.
.
1-9 - different text for each file here.txt
1-10 - different text for each file here.txt
1-11 - different text for each file here.txt
1-12 - different text for each file here.txt
.
.
我想一次性将所有文件重命名(使用zmv
running in zsh
)为:
1-01 - different text for each file here.txt
1-02 - different text for each file here.txt
.
.
1-09 - different text for each file here.txt
1-10 - different text for each file here.txt
1-11 - different text for each file here.txt
1-12 - different text for each file here.txt
以下是我尝试过的:zmv '1-([0-9])(*)' '1-0$1$2'
这将产生以下文件名:
1-01 - different text for each file here.txt
1-02 - different text for each file here.txt
.
.
1-09 - different text for each file here.txt
1-010 - different text for each file here.txt
1-011 - different text for each file here.txt
1-012 - different text for each file here.txt
.
.
因此我认为我应该将查找模式改为 zmv '1-([0-9]{1})(*)' '1-0$1$2'
仅匹配连字符后只有一个数字的文件,但是这不起作用(我只得到zmv:232: no matches found: 1-([0-9]{1})(*)
)。
我做错了什么?这是否可行?zmv
或者我应该使用循环之类的东西sed
吗for
?
答案1
这应该对你有用:
zmv '1-([0-9])( *)' '1-0$1$2'
或者
zmv '1-([0-9])([^0-9]*)' '1-0$1$2'
你只需要让匹配只包含一个数字,后面跟着一个空格或非数字。按照你的做法,它匹配一个数字,后面跟着一个任何事物。