Transfer smaller files and directories

The scp command, short for secure copy, transfers files between local and remote machines using the SSH protocol. This method encrypts all data during transmission to ensure security.

If SSH is installed on your local machine, scp is typically included in most SSH packages. See: What is SSH?

Although scp is straightforward and user-friendly, it lacks advanced features such as delta transfers (which send only the changed parts of files), integrity checks, and the ability to resume interrupted transfers. It is best suited for quick, one-time transfers of small to medium-sized files, like configuration files, scripts, and smaller data sets. For larger transfers or directory synchronization, consider using rsync.

Warning

Files copied with scp that already exist at the target location will be overwritten without confirmation.

Basic syntax

Run the scp command from a terminal on your local machine. Ensure this terminal is not within an SSH session to the instance.

$scp -i <private_key_path> <source_path> <target_path>

Prefix the instance’s username and IP address to the source path for downloads and to the target path for uploads, followed by a colon:

Example of a full instance path
ubuntu@192.0.2.0:/home/ubuntu/

For the private key path, use the local path to the private key for the instance’s SSH key. See: How to use SSH keys.

Tip

If you have recently logged into the instance via SSH from your local machine, your credentials might be cached. In this case, you can omit the -i flag and the private key path.

Transfer a file

To upload a file to an instance, use the following command:

$scp -i <private_key_path> <source_file_path> <username>@<ip_address>:<target_dir_path>/

Example

Upload a file named myfile from the current directory of your local machine to the /home/ubuntu/ directory on the instance:

$scp -i ~/.ssh/id_rsa myfile ubuntu@192.0.2.0:/home/ubuntu/
Warning

You can omit the trailing slash / for the target directory path. However, you must be cautious. If you specify a non-existent directory on the target and omit the trailing slash, the source file will be copied with the directory name as the filename.

Example: Transferring myfile to ubuntu@192.0.2.0:/home/ubuntu/xyz where the directory /home/ubuntu/xyz does not exist will result in myfile being copied to a file named xyz at /home/ubuntu/. If you use ubuntu@192.0.2.0:/home/ubuntu/xyz/ instead, scp will exit without copying.

Transfer a directory

To recursively copy a directory and its contents, use the -r flag followed by the directory path.

To upload a directory to an instance, use the following command:

$scp -i <private_key_path> -r <source_dir_path> <username>@<ip_address>:<target_parent_dir_path>/

Example

Upload a directory named mydir from your local machine to /home/ubuntu/mydir on the instance:

$scp -i ~/.ssh/id_rsa -r mydir ubuntu@192.0.2.0:/home/ubuntu/

Commonly used options

Rename the transferred file

To upload a file and rename it at the target instance, use the following command:

$scp -i <private_key_path> <source_file_path> <username>@<ip_address>:<target_dir_path>/<new_file_name>

Example

Upload a file named file1 from your local machine and rename it to file2 in the /home/ubuntu/ directory on the instance:

$scp -i ~/.ssh/id_rsa file1 ubuntu@192.0.2.0:/home/ubuntu/file2

Rename the transferred directory

To upload a directory and rename it on the target instance, use the following command:

$scp -i <private_key_path> -r <source_dir_path> <username>@<ip_address>:<target_parent_dir_path>/<new_dir_name>

Example

Upload a directory named dir1 from your local machine and rename it to dir2 in the /home/ubuntu/ directory on the instance:

$scp -i ~/.ssh/id_rsa -r dir1 ubuntu@192.0.2.0:/home/ubuntu/dir2

Limit bandwidth

To limit the bandwidth used by scp, use the -l option followed by the limit in kilobits per second (Kbps):

$scp -i <private_key_path> -l <rate_in_Kbps> <source_path> <username>@<ip_address>:<target_path>/

Example

$scp -i ~/.ssh/id_rsa -l 1000 myfile ubuntu@192.0.2.0:/home/ubuntu/

Preserve file attributes

To preserve file modification times, access times, and modes during the transfer, use the -p option:

$scp -i <private_key_path> -p <source_path> <username>@<ip_address>:<target_path>/

Verbose mode

To enable verbose output for debugging purposes, use the -v option:

$scp -i <private_key_path> -v <source_path> <username>@<ip_address>:<target_path>/

Additional resources

View the manual page (manpage) for scp online. Alternatively, enter man scp in a Linux terminal to view the manpage.