Linux – How to Securely Copy Files Using SCP

Secure copy or SCP 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.

Basic secure copy examples:

Copy file from a remote host to local host:

$ scp [email protected]_host:file.txt /local/directory/


Copy file from local host to a remote host:

$ scp file.txt [email protected]_host:/remote/directory/


Copy directory from a remote host to local host:

$ scp -r [email protected]_host:/remote/directory/  /local/directory/


Copy directory from local host to a remote host:

$ scp -r /local/directory/ [email protected]_host:/remote/directory/


Copy file from remote host to remote host:

$ scp [email protected]_host:/remote/directory/file.txt [email protected]_host:/remote/directory/


— Example:  scp -r  [email protected]:/var/www/html/ /home/hydn/backups/test/

— Host can be IP or domain name. Once you click return, you will be prompted for SSH password.

— Although this page covers Linux OS, 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 which already exists, scp will replace the contents of the target file. So be careful.

— SCP is faster and more secure than FTP.

SCP options:

-r Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree traversal.

-C – Compression enable. Passes the -C flag to ssh to enable compression.

Related:  Temporary change #ArchLinux to #Debian

-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. Causes scp and ssh to print debugging messages about their progress. This is helpful in debugging connection, authentication, and configuration problems.