Mac OS X的终端批量重命名 [英] mac os x terminal batch rename

查看:2771
本文介绍了Mac OS X的终端批量重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的文件命名的文件夹:

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 of bash 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 for Pattern 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 in bash - true regular expressions are used.

这篇关于Mac OS X的终端批量重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆