Linux scp Command: Copy Files over SSH

scp copies files between local and remote systems using SSH. The remote location is usually specified in the form user@host:path.

Current OpenSSH scp by default uses the SFTP protocol and only selects the legacy SCP protocol when compatibility with older servers is needed. The copy direction is determined by the order of the source and destination. Be careful with path syntax and quoting when the file name contains colons or special characters interpreted by the remote shell.

Basic syntax

scp [options] source target

The installed implementation and options may vary depending on the distribution. Check the description for your system with man scp.

Examples

Copying local files to the server

Check first, as existing files at the destination path may be overwritten.

scp report.txt alice@server.example.com:/home/alice/

Copying server files to local

Retrieve remote files to the current directory.

scp alice@server.example.com:/home/alice/report.txt .

Copy Directory

Use -r to include subfiles. Check the expected size.

scp -r project/ alice@server.example.com:/home/alice/

Checking Transfer Direction and Results

scp localfile user@host:path copies from local to remote. The following server and path are for example purposes, so check actual permissions and storage space and modify accordingly.

scp report.txt user@server.example.test:/tmp/report.txt

On success, the transfer progress may be displayed, or it may finish without any message depending on output settings. On the remote server, check the file name and size with ssh user@server.example.test 'ls -lh /tmp/report.txt'.

The location before and after the colon determines the transfer direction. Since it can overwrite existing remote files, double-check the destination path. To resume interrupted large transfers or repeatedly synchronize multiple files, rsync may be more suitable.

Main Options and Format

Options/Format Description
-r Recursively copies directories.
-P port Specifies the SSH server port. Unlike ssh's -p, this is uppercase.
-i keyfile Specifies the private key to use for authentication.
-p Preserves file modification time and permissions as much as possible.
-O Uses the legacy SCP protocol if compatibility is needed.

Precautions when using

If the target file exists, it can be overwritten, so check the path and account, and back up important files first. The older protocol of -O has remote shell interpretation and security risks, so it is generally not used. scp is not a tool that synchronizes by comparing only changed files or matches deleted states. For such tasks, rsync may be suitable.

Frequently Asked Questions

Is the port option in scp -p?

No. The server port is uppercase -P. Lowercase -p is the option to preserve file times and permissions.

Official Documentation

The exact behavior of the options and implementation differences can be found in the official scp documentation.