【问题标题】:how to untar the file and rename the folder in one command line operation?如何在一个命令行操作中解压文件并重命名文件夹?
【发布时间】:2011-09-18 16:51:48
【问题描述】:

我想下载一个文件,解压并重命名文件夹。

我可以下载文件并用

解压它
curl https://s3.amazonaws.com/sampletest/sample.tar.gz | tar xz

如何在同一命令中重命名文件夹?

curl https://s3.amazonaws.com/sampletest/sample.tar.gz | tar xz | mv ???????

我不想在命令中明确使用文件夹名称。

【问题讨论】:

  • 当您说不想显式使用文件夹名称时,您的意思是要根据 tar 最初的名称或其他名称重命名文件夹吗?

标签: shell command-line


【解决方案1】:

这是可能的,但不是微不足道的。如果您的 tar(例如 GNU Tar)支持的话,创建您自己的目录,cd 进入它,然后将 --strip-components 1--strip-path 1 传递给 tar 会更容易。

 File name transformations:

      --strip-components=NUMBER   strip NUMBER leading components from file
                                  names on extraction
      --transform=EXPRESSION, --xform=EXPRESSION
                                  use sed replace EXPRESSION to transform file names

【讨论】:

    【解决方案2】:

    如果您的系统没有安装 GNU tar,它可能仍然有 pax(一个 POSIX 工具)可用。后者支持-s 选项,允许任意更改已处理文件的路径名。

    那就是:

    curl https://s3.amazonaws.com/sampletest/sample.tar.gz | gunzip | pax -r -s "/old/new/"
    

    【讨论】:

      猜你喜欢
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多