The Unix/Linux command scp (which stands for “secure copy protocol”) is a simple tool for submitting or downloading files (or directories) to/from a remote computer.
The transfer is done on top of SSH, which is how it preserves its familiar options (like for specifying identities and qualifications) and ensures a protected connection. It’s truly handy to be able to move around files between any system that supports SSH.
Even if you do not already understand how to use the scp command, the scp ought to be a bit more familiar to you thanks to its similarity to ssh. The most significant differences come with specifying file/directory paths.
With scp, you can copy a file or directory:
- From your local system to a remote system.
- From a remote system to your local system.
- Between 2 remote systems from your local system.
Syntax for using the scp command
The general syntax of the Linux scp command with various options is:
scp [OPTION] [user@]Source_Host:]file1 [user@]Destination_Host:]file2
The following options can be used:
–r | In order to copy directories recursively, use this option. |
–q | This option will disable the progress meter. |
–P port | Specifies the port to connect on the remote host. |
–p | Preserves modification times, access times, and modes from the original file. |
–S program | Name of the program to use for the encrypted connection. The program must understand ssh(1) options. |
–v | Verbose mode. This is helpful in debugging connection, authentication, and configuration problems. |
Let us have a look at the usage of scp command now.
An example of Copy the file from a remote host to the local host
The following command will copy the test.txt from the remote system to the local host:
$ scp username@remotehost.com:test.txt /test/local/directory
The command example of local host to a remote server
This command will upload the test.txt file from the local host the remote server.
$ scp test.txt username@remotehost.com:/test/remote/directory
Can I move/copy files from remote to remote systems?
Yes of course, you may use the scp command to copy the file from one remote system to the other. See an example command below:
$ scp username@remosthostsource.com:/test/remote/directory/test.txt \ username@remosthostdest.com:/test/remote/directory/
This will copy the test.txt file from one remote server to another.
Uploading multiple files example to the remote server
This command will upload or copy two files from the local host to the home directory of remote system:
$ scp test1.txt test2.txt username@remotehost.com:~
Downloading a Directory example using –r option
In this use-case, we wish to utilize scp to download a directory from a remote server to our local machine. To accomplish this, we’ll utilize the -r option, which tells scp to recursively copy all of the directory’s contents to our machine.
$ scp -r username@remosthostdest.com:/test/remote/src /path/to/local/dest
You’ll be prompted for your password on the source system. The command won’t work unless you provide the correct password.
Uploading a folder’s contents using -r
The very same exact concepts as downloading a directory site use here as well. You’ll most likely see that the only difference is where we specify the source directory within the actual command.
$ scp -r /path/to/local/src username@remosthostdest.com:/test/remote/dest
Specifying a port example command
As mentioned earlier, you may use the –P option to speicfiy the port. See an example command below:
$ scp -P 2264 test.txt username@remotehost.com:/test/remote/dest