What is scp?

scp stands for Secure Copy Protocol. It is a secure file transfer protocol that copies files to and from hosts. It uses Secure Shell (SSH) to keep the files protected while in transit.

scp is a command line utility, meaning you will have to use Terminal (Mac) or Command Prompt (Windows).

Why use scp?

Since scp uses SSH, the transferred data will be encrypted. This keeps your information from being compromised during transit.

scp is operated by writing single commands into the command line, making it a good option for repeatedly updated files.

Basic Syntax

The basic syntax of scp looks like this:

$ scp source_file_path username@hostname:destination_file_path

And if you were moving file.txt in folder1 on host1 to localFolder in your local Documents:

$ scp folder1/file.txt username@host1:Documents/localFolder

Common scp Commands

-B |Enables batch mode, which keeps scp from asking for passwords.

-C |Enables compression. See common use cases.

Note: Do not confuse with -c, which is lowercase and has an alternate meaning.

-l |Limits the bandwidth used. Uses Kbit/s.

-p |Maintains the access times, modification times, and modes of the original file.

-q |Enables quiet mode, which prevents the progress meter, diagnostic messages, and warning messages from appearing.

-r |Used to copy entire directories recursively. See common use cases.

-v |Enables verbose mode. See common use cases

Common Use Cases

Verbose Output

This is used when a program is giving you problems, such as failing or not completing a request.

The verbose output shows you all the processes going on in the background, thus allowing you to sift through and find where the program is running into a problem.

Add -v when you want to see the verbose output.

$ scp -v/file.txt

Transfer Multiple Files

You can transfer multiple files by putting file names at the beginning:

$ scp file1.txt file2.txt username@remotehost:/destination/file/path/

Or putting them at the end:

$ scp username@host:/destination/file/path/\{file1.txt,file2.txt} .

$ scp root@123.456.7.8:~/\{file1.txt,file2.txt} .

Copy Entire Directory

The -r stands for “recursive.” This is used when you need the application to work as a whole and run through processes a number of times.

This is needed when copying an entire directory.

Add -v if you would like to see the processes working in the background.  

Local to remote:

$ scp -r /local/file/path username@host:/remote/file/path

Remote to local:

$ scp -r username@host:/remote/file/path /local/file/path

Copy Files Across Two Remote Hosts

You can copy files to two remote hosts.

$ scp username1@host1:/remote/file/path/file1.txt username2@host2:/remote/file/path/

Speed Up the Transfer with Compression

The -vrC stand for -v, -r, and -C.  This option can speed up transfer time and save bandwidth. Use -C to enable compression. The files will be decompressed when they reach their destination.

$ scp -vrC username@host:/destination/file/path

Preserve File Attributes

You can preserve the file’s access times, modification times, and modes by using -p.

$ scp -C -p ~/file.txt username@host:/file/path/file.txt