A quick post highlighting some useful syntax for scp and rsync (reproduced from here and here, also Digital Ocean’s guide here)

rsync

You should also consider using rsync instead. rsync compares the data on the sending and receiving sides with a diff mechanism so it doesn’t have to resend data that was already previously sent.

If you are going to copy something to a remote machine more than once, use rsync. Actually, it’s good to use rsync every time because it has more controls for things like copying file permissions and ownership and excluding certain files or directories.

NOTE: The trailing / on source dir. This is necessary to mean “the contents of dir”.
In general to push files to a server:

$ rsync -av /source/dir/ server:/destination/dir

This will synchronize a local directory with a remote directory. If you run it a second time and the contents of the local directory haven’t changed, no data will be transferred – much more efficient than running scp and copying everything every time.

Or from the man page:

Local:  rsync [OPTION...] SRC... [DEST]
Access via remote shell:
  Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
Access via rsync daemon:
  Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
        rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
        rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
Usages with just one SRC arg and no DEST arg will list the source files instead of copying.

Also, rsync allows you to recover from interrupted transfers very easily, unlike scp.

Finally, modern versions of rsync by default run over ssh, so if scp is already working, rsync should pretty much be a drop-in replacement.

scp

$ scp -rp sourcedirectory user@dest:/path
  • -r means recursive
  • -p preserves modification times, access times, and modes from the original file.

scp allows files to be copied to, from, or between different hosts. It uses ssh for data transfer and provides the same authentication and same level of security as ssh.

Examples

Copy the file “foobar.txt” from a remote host to the local host

$ scp your_username@remotehost.edu:foobar.txt /some/local/directory

Copy the file “foobar.txt” from the local host to a remote host

$ scp foobar.txt your_username@remotehost.edu:/some/remote/directory

Copy the directory “foo” from the local host to a remote host’s directory “bar”

$ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar

Copy the file “foobar.txt” from remote host “rh1.edu” to remote host “rh2.edu”

$ scp your_username@rh1.edu:/some/remote/directory/foobar.txt
\your_username@rh2.edu:/some/remote/directory/

Copying the files “foo.txt” and “bar.txt” from the local host to your home directory on the remote host

$ scp foo.txt bar.txt your_username@remotehost.edu:~

Copy the file “foobar.txt” from the local host to a remote host using port 2264

$ scp -P 2264 foobar.txt your_username@remotehost.edu:/some/remote/directory

Copy multiple files from the remote host to your current directory on the local host

$ scp your_username@remotehost.edu:/some/remote/directory/\{a,b,c\} .
$ scp your_username@remotehost.edu:~/\{foo.txt,bar.txt\} .

scp Performance

By default scp uses the Triple-DES cipher to encrypt the data being sent. Using the Blowfish cipher has been shown to increase speed. This can be done by using option -c blowfish in the command line.

$ scp -c blowfish some_file your_username@remotehost.edu:~

It is often suggested that the -C option for compression should also be used to increase speed. The effect of compression, however, will only significantly increase speed if your connection is very slow. Otherwise it may just be adding extra burden to the CPU. An example of using blowfish and compression:

$ scp -c blowfish -C local_file your_username@remotehost.edu:~

Contributions

Thanks Stewart Macleod for port example.