Mac OS X的终端批量重命名 [英] mac os x terminal batch rename
问题描述
我有一系列的文件命名的文件夹:
i have a folder with a series of files named:
prefix_1234_567.png
prefix_abcd_efg.png
我想批量删除一个下划线,中间内容,以便输出将会是
i'd like to batch remove one underscore and the middle content so the output would be
prefix_567.png
prefix_efg.png
谢谢
相关但不是完全的解释:
<一href=\"http://stackoverflow.com/questions/5394112/how-can-i-batch-rename-files-using-the-terminal\">how我可以批量重命名使用终端文件?
<一href=\"http://stackoverflow.com/questions/10664542/regex-to-batch-rename-files-in-os-x-terminal\">Regex批量在OS X的终端重命名文件
推荐答案
在特定的情况下,你可以使用下面的庆典
命令(庆典
是OSX默认的shell):
In your specific case you can use the following bash
command (bash
is the default shell on OSX):
for f in *.png; do echo mv "$f" "${f/_*_/_}"; done
注意回声
是prepended到 MV
,以便进行试运行。删除它来执行实际的重命名。
Note that echo
is prepended to mv
so as to perform a dry run. Remove it to perform actual renaming.
您可以通过命令行或脚本运行它。
You can run it from the command line or a script.
-
$ {F / _ * _ / _}
是庆典
的parameter扩张:(第一)串匹配模式_ * _
替换文字_
,有效地切断从名字中间的标记。 - 注意的模式的使用是的通配符的前pressions,不是的常规的前pressions(运行
男人庆典
并搜索模式匹配
)。
"${f/_*_/_}"
is an application ofbash
parameter expansion: the (first) substring matching pattern_*_
is replaced with literal_
, effectively cutting the middle token from the name.- Note that the pattern used is a wildcard expressions, not a regular expressions (run
man bash
and search forPattern Matching
).
如果你经常发现自己批量重命名文件,可以考虑专门的工具,如基于Perl的 改名
实用。
在OSX你可以使用流行的包管理器安装家酿如下:酿造安装重命名
If you find yourself batch-renaming files frequently, consider a specialized tool such as the Perl-based rename
utility.
On OSX you can install it using popular package manager Homebrew as follows: brew install rename
下面是一个使用重命名等效命令
:
rename -n -e 's/_.*_/_/' *.png
同样,这个命令执行试运行;删除 -n
来执行实际的重命名。
- 类似于
庆典
解决方案,取值/.../.../
执行文本替换,但 - 不像在庆典
- 真正的常规EX pressions 的使用
- Similar to the
bash
solution,s/.../.../
performs text substitution, but - unlike inbash
- true regular expressions are used.
这篇关于Mac OS X的终端批量重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!