使用通配符进行大规模重命名?
我意识到我已经以一种一种方式命名了一堆文件,所以我想将以“1”开头的任何文件重命名为相同的东西,除了以2开头。
例如mv 1.4.5.txt 2.4.5.txt
或mv 1-chart.jpg 2-chart.jpg
等。
我试过mv 1* 2*
但是这个不被接受,因为它将2*
解释为目录。
通配符不会这样做。 看一下echo mv 1* 2*
。 更好的方法是( man rename
):
rename 's/^1/2/' 1*
如果你安装mmv
,你可以用这种方式使用通配符(虽然它们必须被引用 – 这样它们才能被mmv
本身而不是shell解释),并且替换通配符采用#n
forms重新替换第n 个通配符模式:
Usage: mmv [-m|x|r|c|o|a|l] [-h] [-d|p] [-g|t] [-v|n] [from to] Use #[l|u]N in the ``to'' pattern to get the [lowercase|uppercase of the] string matched by the N'th ``from'' pattern wildcard. A ``from'' pattern containing wildcards should be quoted when given on the command line. Also you may need to quote ``to'' pattern. Use -- as the end of options.
所以举个例子
$ mmv -n -- '1*' 2#1 1.sh -> 2.sh : delete old 2.sh? n 1-chart.jpg -> 2-chart.jpg 1.4.5.txt -> 2.4.5.txt 1.csv -> 2.csv
( -n
选项允许您执行干运行 – 将其删除以实际重命名文件。)