How to compress and transfer files in one command

This is something we do pretty much every week so I felt it was worth mentioning here.

It is possible with this simple command to move files from one *nix server to another. "But surely that is just SCP" I hear you say? Well it is, but because of the fact we can "pipe" multiple commands together in Linux, we can also compress these files via tar before we send them (meaning a much faster transfer speed).

The command in question is as follows:

# tar czf - /path/to/send/folder | ssh user@servername.co.uk "cd /path/to/recieve/folder; tar xvzf -"
  1. The command up to the pipe ("|") is to compress all the files in the given folder.
  2. After the pipe we then ssh to our destination server.
  3. Finally, we specify a command to execute on connection, which will CD to the destination location and extract our files.

Thats it, transfer away!