Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'd like to pipe several files to a remote server, piped as input to commands run on the remote server, over ssh, but only one ssh connection/command is permitted. (In the remote authorized_keys file, "command=..." is specified, so only one single command/script can be run on the remote end using that key).

Is it possible to have a setup where multiple files go in the same pipe - I've been considering tar'ing them and having some logic in the remote script/untar to separate the files to separate commands

The initial setup was something like

cat file1 | ssh -i identity 'remote command1 <'
cat file2 | ssh -i identity 'remote command2 <'

Using tar, it could be something like

tar cf - file1 file2 file3 | ssh -i identity 'remote <'

The remote script would need to be able to extract the files one by one and pipe the data into separate commands, but I am not sure how to achieve this

share|improve this question

1 Answer 1

If the boundaries between files don't mater, just cat file1 file2 | ssh …. If they do, use tar. This is actually very close to what it was invented for and precisely what it does.

The other option would be to setup a named pipe that would keep the ssh connection open with a file node for it's input. You could keep dumping things into at will.

Edit: Example usage for tar:

On the receiver end inside the script that gets run as remote command move to where you want to files to be extracted to and then read 'em in:

cd /path/to/extract/destination
cat - | tar x

And on the sending side:

tar c file1 file1 | ssh […]
share|improve this answer
thanks, examples for both would be great! I imagine the local end would be like tar cf - file1 file2 file3 ... | ssh ..., however I am not sure how to handle the files on the other end. tee? – grojo Jun 11 '11 at 19:58
Ok, I realized i left out something crucial, that the files should be piped as input to commands on the remote end! So I want to keep the data in the pipe and never extract to files! Apologies...! – grojo Jun 11 '11 at 20:15
I'd give you an example with a named pipe, but it's exactly the same as a regular pipe except that you set it up ahead of time then write to it when you're ready to. I can't see how this addresses the problem in your question without more details. – Caleb Jun 11 '11 at 20:15
@grojo: This isn't making sense yet. How are you supposed to delineate between files? Does each file that gets sent run a new command on the remote end or can you just concatenate them all together and send them across? – Caleb Jun 11 '11 at 20:16
You could use cat - | tar xO | command on the receiving end to pipe to a command instead of extracting to files, but in that case tar is wasted unless you wanted to compress along the wire because cat could have done the same thing. – Caleb Jun 11 '11 at 20:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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