8 Practical SCP Commands and Examples

SCP stands for secure copy and makes safe file transfers easy on both Windows and Linux. Linux users can take advantage of the built in utility while Windows users can use free software like WinSCP. SCP uses the SSH protocol to encrypt its data transmissions and requires SSH access. Data transfers can take the form of local to remote, remote to local, or remote to remote machines. Simply provide the SSH credentials and you’re good to go. Similar to SSH, if you don’t have the appropriate SSH keys you will be prompted for a password to login to the remote machine(s).

Options

OptionsDescription
-PSpecify SSH port
-pPreserve time stamps
-qQuiet mode. Do not show progress bar.
-rRecursively copy entire directory
-SSpecify program to be used for encrypted connection.
-TDisable file name checking.
-vVerbose mode. Print debugging information.

scp man page

Here are a few practical SCP examples to start transferring files immediately.

1.) Copy file from a local machine to a remote machine

Here we supply SCP with the local file we want copied and then specify the remote machine and directory. The following SCP command transfers the local file backup.tar.gz to a remote directory on remotemachine.com. Replace remotemachine.com with the domain or IP address of your remote host:

scp /home/user/backup.tar.gz user@remotemachine.com:/var/www/backups

2.) Copy file from a remote machine to a local machine

This is basically the same command as example 1 except reversed:

scp user@remotemachine.com:/var/www/backups/backup.tar.gz /home/user

3.) Copy file between 2 remote machines

Here we copy a file from a remote machine to another remote machine. We supply SCP with the addresses of both machines and where we want to put the file:

scp user@192.168.0.1:/var/www/backup.tar.gz user@192.168.0.2:/var/www/

4.) Copy directory from remote machine to local machine

The -r option must be specified when copying directories. Here we copy the contents of the entire /var/www/ directory to our local machine:

scp -r remotemachine.com:/var/www/ /var/www/backup/

5.) Compress files before transferring

SCP can compress files with Gzip before transferring. This can speed up transfers on networks with very slow connections but can slow things down on faster connections. Extra CPU power must be used for the compression.

Here we use the -C option to transfer a file with compression enabled:

scp -C /var/www/novel.txt user@192.168.0.2:/var/www/

6.) Copy multiple local files to remote machine

You can specify multiple files to be copied:

scp /path/to/file1 /path/to/file2 user@192.168.0.2:/var/www/

Alternatively use the following format to copy multiple files with the same extension:

scp {file1,file2,file3}.txt user@192.168.0.2:/var/www/

7.) Copy multiple directories

The following command copies multiple directories in a folder. Sometimes you don’t want the entire directory, just a few folders in that directory. Here we use the -r option once again:

scp -r user@192.168.0.1:/var/www/\{dir1,dir2,dir3\} /local/folder/

8.) Specify SSH port

Specify the port of the remote machine if SSH is not running on the standard port 22:

scp -P 2244 user@192.168.0.2:/var/www/ /path/to/local/folder/

Leave a Comment