scp allows you to securely copy files locally or remotely across a network. It uses SSH for data transfer and uses the same authentication. If you do not have public key authentication enabled you will be prompted for a password.
This basic format for scp is this.
scp [options] [[user@]src_host1:]file1 [[user@]dest_host2:]file2
Assuming the remotehost has a username which matches username on the local host, this command will copy a a local file into the /tmp directory on remotehost. Notice that the colon separates the host from the location.
$ scp file.txt remotehost:/tmp/
You will be presented with the real-time statistics about the file transfer.
$ scp file.txt remotehost:/tmp/
file.txt 100% 22KB 21.9KB/s 00:00
This command is similar but the period instructs the file to be placed in the users home directory.
$ scp file.txt remotehost:.
You can specify another user and login using their username. This will copy the file to ryan’s home directory.
$ scp file.txt ryan@remotehost:.
Alternatively you can copy a remote file to your local host. The period in the destination path refers to the current working directory in this case.
$ scp ryan@remotehost:file.txt .
Likewise, you can copy to any other local path you have access, such as the /tmp directory.
$ scp ryan@remotehost:file.txt /tmp/
Copying directories is similar except that the ‘-r’ option is required. This command will copy a directory from the current working directory to the users home directory on remotehost.
$ scp -r mydir/ remotehost:.
You can even copy from one remote host to another.
$ scp remotehost1:file.txt remotehost2:file.txt
Use the -p option to preserve modification times, access times, and modes from the original file.
$ scp -p remotehost1:/tmp/file.txt remotehost2:/tmp/file.txt