在OSX终端中批量重命名文件 [英] Batch renaming files in OSX terminal

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

问题描述

我正在尝试重命名文件,例如screen-0001.tif0001.tif,使用此SO问题中的方法:

I'm trying to rename files e.g. screen-0001.tif to 0001.tif using the approach in this SO question:

for file in *.tif
do
  echo mv "$file" "${screen-/file}"
done

无法更改任何内容.感谢我出了错的想法.

fails to change anything. Grateful for an idea where I'm going wrong.

推荐答案

两件事:

  1. 您正在回显命令,而不是实际执行它们.在执行大规模重命名时,我将执行此操作,以确保命令正确运行.我可以将输出重定向到一个文件,然后将该文件用作Shell脚本.
  2. 替换错误.有两种方法:
  1. You're echoing the commands and not actually executing them. I will do this when I do massive renames just to make sure that the command works correctly. I can redirect the output to a file, and then use that file as a shell script.
  2. The substitution is wrong. There are two ways:
  1. 最左边的过滤器${file#screen-}.
  2. 替换:${file/screen/}
  1. Left most filter ${file#screen-}.
  2. Substitution: ${file/screen/}

环境变量的名称始终排在最前面.然后是模式类型,然后是模式

The name of the environment variable always goes first. Then the pattern type, then the pattern

这是我要这样做的方式:

Here's how I would do this:

$ for file in *.tif
> do
>   echo "mv '$file' '${file#screen-}'"
> done | tee mymove.sh   # Build a shell script 
$ vi mymove.sh           # Examine the shell script and make sure everything is correct
$ bash mymove.sh         # If all is good, execute the shell script.

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

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