This post includes SCP examples. SCP or secure copy allows secure transferring of files between a local host and a remote host or between two remote hosts. It uses the same authentication and security as the Secure Shell (SSH) protocol from which it is based. SCP is loved for its simplicity, security and pre-installed availability.
Copy file from a remote host to local host SCP example:
$ scp username@from_host:file.txt /local/directory/
Copy file from local host to a remote host SCP example:
$ scp file.txt username@to_host:/remote/directory/
Copy directory from a remote host to local host SCP example:
$ scp -r username@from_host:/remote/directory/ /local/directory/
Copy directory from local host to a remote hos SCP example:
$ scp -r /local/directory/ username@to_host:/remote/directory/
Copy file from remote host to remote host SCP example:
$ scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/
— SCP example:
scp -r firstname.lastname@example.org:/var/www/html/ /home/hydn/backups/test/ Also, see Backup solutions.
— Host can be IP or domain name. Once you click return, you will be prompted for an SSH password.
— Although this page covers SCP Linux, the instructions will also work for Mac using “Terminal.” You can also use WinSCP to accomplish this on a Windows PC/server.
— When copying a source file to a target file that already exists, SCP will replace the contents of the target file. So be careful.
Screenshot: output of “man scp” command.
–r Recursively copy entire directories. Note that this follows symbolic links encountered in the tree traversal.
-C Compression enable. Passes the -C flag to ssh to enable compression.
-l limit – Limits the used bandwidth, specified in Kbit/s.
-o ssh_option – Can be used to pass options to ssh in the format used in ssh_config.
-P port – Specifies the port to connect to on the remote host. Note that this option is written with a capital ‘P.’
-p Preserves modification times, access times, and modes from the original file.
-q Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh.
-v Verbose mode. Print debugging messages about progress. This is helpful in debugging connection, authentication, and configuration problems.
Have a look at rsync:
Also, see Linux Networking commands and scripts.
Published: September 16th, 2013 | Last updated: May 19th, 2022