I have a tar.gz file that has multiple dirs and files inside. I need to copy and extract a specific dir from it to a remote host.

I know how to do it with a single file, but have trouble with multiple files in a single command:

    tar zxOf /mnt/debug.tar.gz libr.so | ssh $MY_IP 'cat > /local/libr.so'

I currently workaround by extracting the whole dir locally, then use scp to copy:

tar -zxf /mnt/debug.tar.gz -C utils/foo --strip-components=1 tools/


  • 2
    If you don't mind the network overhead, you can do stuff like cat debug.tar.gz | ssh remotehost tar xfz - -C /local foo bar baz - ie stream the tar file to the remote host and do the tar extraction there. It doesn't use disk space, but it does use network bandwidth. Aug 22 '16 at 12:44
  • I thought of this option, but the whole tar is quite large. Aug 22 '16 at 13:34

How about a simple shell loop:

files="first second third"
for f in files; do
  tar xf thearchive.tar $f | ssh otherhost "cat > $f"

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.