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.

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.

  • I was not clear in my original posting :) I need to extract more than one member, plus a directory. – Felipe Alvarez Aug 30 '11 at 8:55
  • should be tar -xz0f : after f comes archive name – Felipe Alvarez Aug 30 '11 at 8:56
  • 1
    Extracting 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 Aug 30 '11 at 9:18
  • ahhhhh, I see. So there is no "easy" one-liner method. Thanks :-) – Felipe Alvarez Aug 30 '11 at 9:27
  • 2
    Alternatively, 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 Apr 15 '15 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.