Copy files with SCP and Rsync

This article describes how to transfer a file from and to a remote server.

Copying files and directories with SCP or Rsync

You can use SecureShell (SSH) or Remote Sync (Rsync) to transfer files to a remote server.
Secure Copy (SCP) uses SSH to copy only the files or directories that you select. On first
use, Rsync copies all files and directories and then it copies only the files and directories
that you have changed. It does not copy all the files and directories again.

SSH and SCP examples

Copy a file from a remote server:

~$ scp [email protected]:/path/file_name /local/destination/path/

Copy a directory from a remote server:

~$ scp -r [email protected]:/path/directory[/] /local/destination/path/

Copy a file to a remote server:

~$ scp /local/path/file_name [email protected]:/destination/path/

Copy a directory to a remote server:

~$ scp  -r /local/path/directory[/]  [email protected]:/destination/path/

Rsync examples

Because Rsync transfers files recursively, you do not need to add the -r
flag. You can use the following commands to transfer the files in an archived
or compressed manner:

  • -a or --archive: Like recursion, this option preserves source characteristics, such as permissions.
  • -v or --verbose: This option shows you more information during the transfer.
  • -z or --compress: With this option, Rsync compresses the file data sent to the destination machine.

Copy a file from a remote server:

	~$ rsync [-avz] [email protected]:/path/file_name /local/destination/path/

Copy a directory from a remote server:

	~$ rsync [-avz] [email protected]:/path/directory[/] /local/destination/path/

Copy a file to a remote server:

	~$ rsync [-avz] /local/path/file_name [email protected]:/destination/path/

Copy a directory to a remote server:

	~$ rsync [-avz] /local/path/directory[/]  [email protected]:/destination/path/

Trailing slash on source path

A trailing slash ( / ) on the source path changes the transfer behavior to
avoid creating an additional directory level at the destination. With the slash,
Rsync copies the directory content without creating a new folder. Without the slash,
it creates a new directory with the source directory name. Following are examples with
and without the slash:

~$ rsync [-avz] /local/path/directory/ [email protected]:/destination/path/

~$ rsync [-avz] /local/path/directory [email protected]:/destination/path/

Use the Feedback tab to make any comments or ask questions. You can also start a conversation with us.