Piping Output Over a Secure Shell (SSH) Connection

We all know about using scp to transfer files over a secure shell connection. It works fine, but there are many cases where alternate modalities of usage are required, for example, when dealing when you want to transfer the output of one program directly to be stored on a remote machine. Here are some ways of going about doing this. Let “$PROG” be a program that writes data to the standard output stream. Then:

  • Transfering without compression:

    $PROG | ssh destination.ip.address 'cat > ~/file.txt'
  • Using gzip for compression:

    $PROG | gzip -f | ssh destination.ip.address 'gunzip > ~/file.txt'
  • Better compression can usually be achieved by bzip2

    $PROG | bzip2  | ssh destination.ip.address 'bunzip2 > ~/Scratch/file.txt'

I find this useful enough to source the following function into all my shells:

## xof #######################################################################
# Pipe from standard input to remote file.
xof()

And when I want to get fancy, I pipe the output directly to my favorite text editor, BBEdit:

## xobb #######################################################################
# Pipe from standard input to bbedit.
xobb()

On a tangential note, if you have a large number of files that you want to transfer, the following is more efficient than separately tar-ing and scp-ing:

tar cf - *.t | ssh destination.ip.address "tar xf - -C /home/jeet/projects/bbi2
Share