how to untar the file and rename the folder in one

2019-02-25 13:07发布

I want to download a file, untar it and rename the folder.

I am able to download the file and untar it with

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

How can I rename the folder in the same command?

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

I do not want to use the folder name explicitly in the command.

2条回答
Lonely孤独者°
2楼-- · 2019-02-25 13:31

It's possible, but not trivial. It's easier to create your own directory, cd into it, then pass --strip-components 1 or --strip-path 1 to tar if your tar (e.g. GNU Tar) supports it.

 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
查看更多
Anthone
3楼-- · 2019-02-25 13:32

If your system hasn't GNU tar installed, it might still have pax (a POSIX tool) available. The latter supports the -s option which allows arbitrary changes in the path name of the processed files.

That would then be:

curl https://s3.amazonaws.com/sampletest/sample.tar.gz | gunzip | pax -r -s "/old/new/"
查看更多
登录 后发表回答