How to use scp
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.
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:
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
Upload a file
Download a file
To upload a file to an instance, use the following command:
Example
Upload a file named myfile
from the current directory of your local machine to the /home/ubuntu/
directory on the instance:
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.
Upload a directory
Download a directory
To upload a directory to an instance, use the following command:
Example
Upload a directory named mydir
from your local machine to /home/ubuntu/mydir
on the instance:
Commonly used options
Rename the transferred file
Rename a file during upload
Rename a file during download
To upload a file and rename it at the target instance, use the following command:
Example
Upload a file named file1
from your local machine and rename it to file2
in the /home/ubuntu/
directory on the instance:
Rename the transferred directory
Rename a directory during upload
Rename a directory during download
To upload a directory and rename it on the target instance, use the following command:
Example
Upload a directory named dir1
from your local machine and rename it to dir2
in the /home/ubuntu/
directory on the instance:
Limit bandwidth
To limit the bandwidth used by scp, use the -l
option followed by the limit in kilobits per second (Kbps):
Example
Preserve file attributes
To preserve file modification times, access times, and modes during the transfer, use the -p
option:
Verbose mode
To enable verbose output for debugging purposes, use the -v
option:
Additional resources
View the manual page (manpage) for scp
online. Alternatively, enter man scp
in a Linux terminal to view the manpage.