Basic Syntax
The basic syntax for the SCP command is provided below:
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.
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.
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:
In the example below, we are copying the file sales.pdf from the remote system to the local system:
Similarly, you can copy a directory from the remote host to the local system using the -r flag as shown.
For more options on the usage of the SCP command, visit the man pages:
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.