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

thanks relevant but not completely explanatory: how can I batch rename files using the Terminal? Regex to batch rename files in OS X Terminal

谢谢相关但没有完全说明:如何使用终端批量重命名文件?在OS X终端中批量重命名文件。

7 个解决方案

#1


166  

In your specific case you can use the following bash command (bash is the default shell on macOS):

在您的特定情况下,您可以使用以下bash命令(bash是macOS上的默认shell):

for f in *.png; do echo mv "$f" "${f/_*_/_}"; done

Note: echo is prepended to mv so as to perform a dry run. Remove it to perform actual renaming.

注意:回波在mv中被预置,以执行一次排练。删除它以执行实际的重命名。

You can run it from the command line or use it in a script.

您可以从命令行运行它,或者在脚本中使用它。

  • "${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.
  • “${f/_*_/_}”是bash参数扩展的一个应用程序:(第一个)子字符串匹配模式_*_被替换为文字_,有效地从名称中删除中间标记。
  • Note that _*_ is a pattern (a wildcard expression, as also used for globbing), not a regular expression (to learn about patterns, run man bash and search for Pattern Matching).
  • 注意,_*_是一个模式(一个通配符表达式,也用于globbing),而不是正则表达式(了解模式、运行man bash和搜索模式匹配)。

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

如果您发现自己频繁地重命名文件,请考虑一个专门的工具,例如基于perl的rename实用工具。在OSX上,您可以使用流行的包管理器Homebrew来安装它:brew安装rename。

Here's the equivalent command using rename:

下面是使用重命名的等效命令:

rename -n -e 's/_.*_/_/'  *.png

Again, this command performs a dry run; remove -n to perform actual renaming.

同样,这个命令执行一个dry run;删除-n以执行实际的重命名。

  • Similar to the bash solution, s/.../.../ performs text substitution, but - unlike in bash - true regular expressions are used.
  • 类似于bash解决方案,s/…/执行文本替换,但不像在bash中使用的正则表达式。

#2


74  

To rename files, you can use the rename utility:

要重命名文件,可以使用rename实用程序:

brew install rename

酿造安装重命名

For example, to change a search string in all filenames in current directory:

例如,要更改当前目录中所有文件名中的搜索字符串:

rename -nvs searchword replaceword *

Remove the 'n' parameter to apply the changes.

删除“n”参数以应用更改。

More info: man rename

更多信息:重命名

#3


9  

You could use sed:

您可以使用sed:

ls * | sed -e 'p;s@_.*_@_@g' | xargs -n2 mv

result:

结果:

prefix_567.png prefix_efg.png

*to do a dry-run first, replace mv at the end with echo

*首先做一个干跑,最后用回声代替mv。

#4


6  

I had a batch of files that looked like this: be90-01.png and needed to change the dash to underscore. I used this, which worked well:

我有一批文件看起来是这样的:be90-01。png和需要改变破折号以下划线。我用了这个,效果很好:

for f in *; do mv "$f" "`echo $f | tr '-' '_'`"; done

#5


2  

you can install rename command by using brew. just do brew install rename and use it.

您可以通过使用brew来安装rename命令。只需进行brew安装,然后重新命名并使用它。

#6


1  

Using mmv

使用mmv

mmv '*_*_*' '#1_#3' *.png

#7


1  

try this

试试这个

for i in *.png ; do mv "$i" "${i/remove_me*.png/.png}" ; done

Here is another way:

这是另一种方式:

for file in Name*.png; do mv "$file" "01_$file"; done
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/06/08/354f802f8e502d1b3a5eb92338c29227.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告