Say I have an http or tcp server running, which serves tarballs (.tgz files).

Is there some way I can receive files individually so I can do something like this:

nc localhost 5000 | how can I read multiple files here and save each to disk?

to explain further, when I connect to the server, I'd like it to respond with a series of .tgz files. My question is, if it's a tcp server, how can I write each file so that it comes in separately.

The only thing I would know how to do would be to stream the data to a single file, but I don't know how to write out multiple files.

This is potentially an answer: https://stackoverflow.com/a/44894223/1223975

But I don't understand how to write each file out individually.

  • 1
    http multipart might be the way, if pure tcp makes this hard. one problem is that the client does not know in advance how many files it will receive, the server dictates that. – Alexander Mills May 14 '18 at 6:17
  • Do you control the server? – muru May 14 '18 at 6:59
  • yeah I do, I control both server and client, it's all locally running - I think this answer might work: stackoverflow.com/a/44894223/1223975 – Alexander Mills May 14 '18 at 7:07
  • but I guess after tar -x runs, I don't know how to save each file to disk, or how that works – Alexander Mills May 14 '18 at 7:07
  • Yes, that should work. That's what I usually use (either that or a variation of ssh tar c ... | tar x ... if security is needed). tar will unarchive each file in the incoming archive to separate files. – muru May 14 '18 at 7:08

On the server, do:

tar c file1 file2 dir1 file3 ... | nc -l 5000

Then, on the client, do:

nc server 5000 | tar x

Or, slower, but more secure:

ssh server tar c file1 file2 dir1 file3 ... | tar x

For example:

$ ssh localhost 'cd /etc; tar c passwd nsswitch.conf' | (d=$(mktemp -d); tar xv -C "$d"; ls -l "$d"; rm -r "$d")
total 8
-rw-r--r-- 1 muru muru  529 Feb 16  2017 nsswitch.conf
-rw-r--r-- 1 muru muru 2631 Apr 24 18:18 passwd

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.