scp and rsync reference

scp and rsync reference

Similar to my previous post on SSH tunnelling, this post is a quick reference for typical rsync and scp usage.


scp's syntax is as follows:

scp [options] source target

Where options is any scp argument, and source and target can be either network hosts (hostnames or IP addresses), or directories. For specifying the directory of a remote host, you append a colon after the hostname, and type the path. For example: host:/etc/mypath.

Note that scp obeys any configuration specified on ~/.ssh/config or /etc/ssh/ssh_config. For example, if you defined a host test123 with a username, and a port, you can use “test123” in place of the host, without needing to use the full syntax like scp -P 1234 user@test123 {...}.

Basic options

  • -P is used for specifying the port, in case the remote SSH host is using a port other than 22
  • -v turns on verbose mode
  • -q turns on quiet mode
  • -r enables recursively copying entire directories
  • -o specifies SSH options in the usual format
  • -i specifies an identity file
  • -C enables compression

Using scp

Here are three examples. Any of the flags specified above will work in these examples.

Copy a local file to a remote host

scp /etc/mypath/file remote_host:/etc/somepath

This copies the local file /etc/mypath/file into remote_host under /etc/somepath/file.

Copy a remote file from a remote host

scp remote_host:/etc/mypath/file /etc/somepath

This copies the remote file /etc/mypath/file from remote_host into the local filesystem /etc/somepath/file.

Copy an entire directory from a remote host to the local filesystem

scp -r remote_host:/etc/mypath /etc/remote_host_dir/


rsync's syntax is as follows:

rsync [options] source target

Where options is any rsync argument, and source and target work similarly to scp as explained above.

Again, just like scp, rsync follows SSH custom config files.

Basic options

  • -v enables verbose mode
  • -q turns on quiet mode
  • -r enables recursively copying entire directories
  • -n execute a “dry run”, that is, run once to print changes, then exit without transferring anything
  • -P shows progress during transfer
  • -a enables attribute preservation
  • -z enables compression
  • -c enables checksum checking, which tells rsyn what files to skip based on a checksum instead of modification time and size
  • -h enables human-readable number format
  • –delete files that have been deleted from the source since the last run, useful for backing up, this effectively turns rsync into a sync tool, without which it is just a file transfer tool with checksums and directory diff transfers

Using rsync

Sync from the local filesystem to a remote host

rsync -r /etc/mypath remote_host:/etc/somepath

This copies the /etc/mypath directory and all its files and subdirectories to the remote /etc/somepath directory.

Sync a remote host to the local filesystem

rsync -r remote_host:/etc/mypath /etc/somepath

This copies the remote /etc/mypath directory on remote_host to /etc/somepath on the local filesystem.

Practical example: downloading a remote directory with recursive, verbose, checksum, human-readable, and progress arguments, as well as deleting files that no longer exist on the source

rsync -rvchP --delete /mnt/remotebackup/storage

Wrapping up

scp and rsync are powerful tools that make use of SSH to quickly transfer files and even entire directories.

I like using scp for one-off single file transfer, whereas rsync has many more options you can read in the manual, making it an ideal candidate for backup cronjobs.