scp Command: Tutorial & Examples
Copy files to or from a remote host using the Secure Copy (SCP) protocol
The scp
command is used to securely copy files and directories between two different systems over a network connection. It uses the SSH (Secure Shell) protocol for secure
transfer of files, and is similar to the cp
command but allows for remote file transfers.
The basic syntax for using the scp
command is as follows:
scp [options] [source] [destination]
Here are a few examples of how the scp
command can be used:
Copy a file named file.txt
from the local system to a remote system:
scp file.txt user@remote-host:/path/to/destination
Copy a directory named mydir
and its contents from the remote system to the local system:
scp -r user@remote-host:/path/to/mydir .
In the first example, the scp
command is used to copy a file named file.txt
from the local system to a remote system. The user's login name and remote host name or IP address
are specified as part of the destination path, as well as the directory where the file should be copied. The second example is using the -r
option to copy recursively the
directory mydir
and it's contents from the remote system to the local system.
scp
uses the same authentication and security mechanisms as ssh
, and it can be configured to use specific ssh key, and it's also possible to specify a
different ssh port by providing it in the destination path.
It's worth noting that the scp
command is a powerful tool for transferring files securely between systems and it's widely used in many scenarios such as deployment, backups, and
data transfer between servers.
An alternative to scp
is rsync
which only transfers the differences in a file or directory, thus saving a lot of bandwidth on slow connections.