简体   繁体   English

Bash命令行重命名通配符

[英]Bash command-line to rename wildcard

In my /opt/myapp dir I have a remote, automated process that will be dropping files of the form <anything>-<version>.zip , where <anything> could literally be any alphanumeric filename, and where <version> will be a version number. 在我的/opt/myapp目录中,我有一个远程的自动化过程,该过程将删除格式为<anything>-<version>.zip ,其中<anything>可以是任何字母数字文件名,而<version>是版本号。 So, examples of what this automated process will be delivering are: 因此,该自动化流程将提供的示例如下:

  • fizz-0.1.0.zip
  • buzz-1.12.35.zip
  • foo-1.0.0.zip
  • bar-3.0.9.RC.zip

etc. Through controls outside the scope of this question, I am guaranteed that only one of these ZIP files will exist under /opt/myapp at any given time. 通过此问题范围之外的控件,可以保证在任何给定时间,这些压缩文件中只有一个在/opt/myapp下。 I need to write a Bash shell command that will rename these files and move them to /opt/staging . 我需要编写一个Bash shell命令 ,该命令将重命名这些文件并将其移至/opt/staging For the rename, the ZIP files need to have their version dropped. 对于重命名,ZIP文件需要删除其版本。 And so /opt/myapp/<anything>-<version>.zip is renamed and moved to /opt/staging/<anything>.zip . 因此,/ /opt/myapp/<anything>-<version>.zip被重命名并移至/opt/staging/<anything>.zip Using the examples above: 使用上面的示例:

  • /opt/myapp/fizz-0.1.0.zip => /opt/staging/fizz.zip /opt/myapp/fizz-0.1.0.zip => /opt/staging/fizz.zip
  • /opt/myapp/buzz-1.12.35.zip => /opt/staging/buzz.zip /opt/myapp/buzz-1.12.35.zip => /opt/staging/buzz.zip
  • /opt/myapp/foo-1.0.0.zip => /opt/staging/foo.zip /opt/myapp/foo-1.0.0.zip => /opt/staging/foo.zip
  • /opt/myapp/bar-3.0.9.RC.zip => /opt/staging/bar.zip /opt/myapp/bar-3.0.9.RC.zip => /opt/staging/bar.zip

The directory move is obvious and easy, but the rename is making me pull my hair out. 目录的移动是显而易见且容易的,但是重命名使我不寒而栗。 I need to somehow save off the <anything> and then re-access it later on in the command. 我需要以某种方式保存<anything> ,然后稍后在命令中重新访问它。 The command must be generic and can take no arguments. 该命令必须是通用的,并且不能包含任何参数。

My best attempt (which doesn't even come close to working) so far is: 到目前为止,我最好的尝试(甚至还差点没用)是:

file=*.zip; 文件= *拉链。 file=?; 文件= ?; mv file /opt/staging mv文件/ opt / staging

Any ideas on how to do this? 有关如何执行此操作的任何想法?

for file in *.zip; do
  [[ -e $file ]] || continue # handle zero-match case without nullglob
  mv -- "$file" /opt/staging/"${file%-*}.zip"
done

${file%-*} removes everything after the last - in the filename. ${file%-*}删除后,最后一切-在文件名。 Thus, we change fizz-0.1.0.zip to fizz , and then add a leading /opt/staging/ and a trailing .zip . 因此,我们将fizz-0.1.0.zip更改为fizz ,然后添加前导/opt/staging/和后缀.zip


To make this more generic (working with multiple extensions), see the following function (callable as a command; function body could also be put into a script with a #!/bin/bash shebang, if one removed the local declarations): 要使其更通用(使用多个扩展名),请参见以下函数(可作为命令调用;如果删除了local声明,则函数主体也可以放入带有#!/bin/bash shebang的脚本中):

stage() {
  local file ext
  for file; do
    [[ -e $file ]] || continue
    [[ $file = *-*.* ]] || {
      printf 'ERROR: Filename %q does not contain a dash and a dot\n' "$file" >&2
      continue
    }
    ext=${file##*.}
    mv -- "$file" /opt/staging/"${file%-*}.$ext"
  done
}

...with that function defined, you can run: ...定义了该功能后,您可以运行:

stage *.zip *.txt

...or any other pattern you so choose. ...或您选择的其他任何模式。

f=foo-1.3.4.txt
echo ${f%%-*}.${f##*.}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM