how to use secure copy (scp) to copy files in linux

If you work with computers of any kind, sooner or later you will want to copy files between folders. Often times you want to copy files between machines or computers over a network as well.

Secure Copy or scp allows you to copy files between hosts on a network. The files are copied over ssh or secure shell which allows it to use the same authentication and same level of security used by ssh.

scp can also copy files between folder on the same machine. Because of its inherent security it is a good idea to get into the habit of using scp instead of copy or cp to transfer files.

We will look at some examples for commonly used cases and some of the commonly used options. Assume that local_host refers to the host machine that you are currently logged into and remote_host as the host name of the machine you are copying the files to.

The general syntax of the scp command is

scp <options> <username>@<from_hostname>:/path <username>@<to_hostname>:/path

The scp command will prompt you for a password if one is required. You do not have to specify the username if the username is the same as the currently logged in user or is the user you are executing the command as.

copy files from local host to remote host

bash$ scp -v /path/to/file.txt [email protected]_host:/path/to/dir/ 

This will copy the file named file.txt to a remote directory (eg. /path/to/dir) on the remote host machine. You can use wildcard to match multiple files if you want to transfer multiple files, just as the cp command.

copy files from remote host to local host

bash$ scp -v [email protected]_host:/path/to/file.txt /path/to/dir/

You just need to swap the source and destination arguments to transfer files from the remote host to the local host.

copy folder from local host to remote host

bash$ scp -rv /path/to/local/dir/ [email protected]_host:/path/to/remote/dir/

You can use the -r command line option to copy folders recursively. By default the scp will follow the symbolic links encountered in the folder traversal.

copy folder from remote host to local host

bash$  scp -rv [email protected]_host:/path/to/remote/dir /path/to/local/dir/

In order to copy folder and folder content recursively to the local host, you just swap the source and destination arguments of the command.

copy folder from remote host to another remote host

You can use this command to transfer files and folders from one remote host to another as well. This will transfer the files directly between the two remote machines.

bash$ scp -rv [email protected]_host1:/path/to/remote1/dir/ [email protected]_host2:/path/to/remote2/dir/

copy folder and files on the same local host

bash$ scp -rv /path/to/local1/dir/ /path/to/local2/dir/

This is the same as using the cp command. You could use this as well just so that you get into the habit of using the scp command.

There are several handy command line options that you can use with the scp command.

  • -r : as we have seen above, the -r option will recursively copy the entire directories
  • -v : the verbose option will print out the operations that are being executed.
  • -q : the quiet mode that disables the progress meter and other warnings from scp and ssh
  • -C : enable compression. This is beneficial if you are transferring large files across the network.