extract a few members from tar archive and pipe through network

9

2

edit: I want to extra member01 and member02 and directory blah/

tarball_1.tar.gz contains directory test/ with 20 files. I want to extract only member test/member01 and test/member02 and directory blah/ and copy them to another "remote_host" using ssh/scp.

Can this be done as a one-liner? I considered using tar, pax, or cpio but I guess I'm not very skilled with these utilities yet.

Felipe Alvarez

Posted 2011-08-30T08:17:39.913

Reputation: 1 666

Answers

15

tar -xzOf file.tar.gz file_you_want_to_extract | ssh user@host 'cat > /path/to/destination_file'
  • -x : Extract
  • -z : Through gzip
  • -f : Take in a file as the input.
  • -O : Extract to stdout

The file_you_want_to_extract is extracted from file.tar.gz to the standard output, piped into ssh, which runs cat on the remote host and writes its standard in to the remote destination_file. Of course, you'll want to ensure you have write permission to your desired destination file on the remote host.

atanamir

Posted 2011-08-30T08:17:39.913

Reputation: 436

2Alternatively, if "one-liner" is more important than "how long it takes", you can pipe the whole archive through ssh and extract the members you want on the other side "cat file.tar.gz | ssh user@host 'tar zxvf file1 file2 dir1' – Colin – 2015-04-15T03:29:55.450

I was not clear in my original posting :) I need to extract more than one member, plus a directory. – Felipe Alvarez – 2011-08-30T08:55:41.880

should be tar -xz0f : after f comes archive name – Felipe Alvarez – 2011-08-30T08:56:56.480

1Extracting multiple members will get messy if you want them to be one-liners, since extracting multiple files to stdout doesn't quite make sense. You'll probably have to tar for each one you want to extract, then use scp -r member1 member2 blah user@host:/destination/folder/ to copy them. If you really want to make it one-line, you can concatenate all those commands with &&. A more practical option is to just make a script as well that iterates through the command-line options and executes tar for each one and then scps all of them at the end. – atanamir – 2011-08-30T09:18:12.087

ahhhhh, I see. So there is no "easy" one-liner method. Thanks :-) – Felipe Alvarez – 2011-08-30T09:27:09.207