Linux Commands

The SCP Command

The SCP command, an acronym for secure copy, is a command used for securely copying files to and from a remote system over SSH protocol. Riding on SSH means that it enjoys the same level of data encryption that SSH provides and is thus considered a safe way of transferring files across two remote hosts. This tutorial will focus on the SCP command and highlight a few example usages.

Basic Syntax

The basic syntax for the SCP command is provided below:

$ scp [option] /path/to/source/file
user@server-IP:/path/to/destination/directory

Where:

/path/to/source/file – This is the source file that you intend to copy to the remote host.

user@server-IP: – This is the username and IP address of the remote system. Take careful note of the colon that appears after the IP address. 

/path/to/destination/directory: – This is the destination directory on the remote system where the file will be copied to.

The SCP Command Also Comes With the Following Command Options

-C – This compresses the file or directory during the copying process.

-P –  Use this option to specify the SSH port if the default SSH port is not set to 22.

-r –  This option recursively copies a directory alongside its contents.

-r –  Preserves the access and modification times of the file being copied.

Copy a File to a Remote Linux Server

To copy a file with no options, simply use the syntax shown. In the example below, I am copying the zip file  nextcloud-21.0.1.zip to the remote host’s home directory  /home/bob. The IP of the host is  192.168.2.103 and bob is the login user.

$ scp nextcloud-21.0.1.zip  [email protected]:/home/bob

To copy a directory, invoke the -r flag as shown. Here, we are copying the bashtop directory to the home directory of the remote node.

$ scp -r bashtop  [email protected]:/home/bob

Copy a File From a Remote System to a Local System

Additionally, one can copy a file from the remote node to the local system as shown:

$ scp [option] user@server-IP:/path/to/source/file/path/to/local/directory

In the example below, we are copying the file sales.pdf from the remote system to the local system:

$ scp [email protected]:/home/bob/sales.pdf  /home/winnie

Similarly, you can copy a directory from the remote host to the local system using the -r flag as shown.

$ scp -r [email protected]:/home/bob/reports_2020  /home/winnie

For more options on the usage of the SCP command, visit the man pages:

$ man scp

Conclusion

The Linux SCP command is a convenient and secure way of transferring files between two remote nodes without having to worry about attackers snooping on your data.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.