Arvids Blog

Thoughts on programming and more

rsync is the better SCP

How are you transferring files to remote servers, only accessible via SSH? Are you still using SCP? Are you tired of watching the file upload with 23 KiB/s? Fear no more! rsync to the rescue!

rsync is cp supercharged. It’s fast, correct, and can be used to copy/move/archive files to remove machines via SSH.

Instead of

scp -i path/to/key.pem" $FILES_TO_UPLOAD $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH

use

rsync -avzh --info=progress2 -e "ssh -i path/to/key.pem" $FILES_TO_UPLOAD $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH

Of course, in typical fashion, you can also download files from the remote, by switching the order of the last arguments.

Let’s quickly walk through the arguments to rsync before I present you with a simple shell alias that simplifies this:

rsync has many more useful options, consult man rsync for more information.

To simplify remembering the rsync command-line (or, avoid RSI by typing too much), you can use the function below to simplify it:

# usage: rscp -i path/to/key $SRC $DEST
rscp () {
    local sshflags=""
    for arg in "$@"; do
        case "$arg" in -*) sshflags="$sshflags $arg"; shift; sshflags="$sshflags $1"; shift;; esac
    done

    rsync -avzh --info=progress2 -e "ssh ${sshflags}" "$@"
}

Add it to your .zshrc or .bashrc/.bash_profile and invoke like scp:

rscp -i path/to/key.pem" $FILES_TO_UPLOAD $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH