extract a few members from tar archive and pipe through network

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.


Solution 1:

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.