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.

What is the scp command?

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.

More in This Category
Linux ps Command: Inspect Running Processes

Linux ps Command: Inspect Running Processes

Learn how to inspect Running Processes with the Linux ps command, including practical examples, key options, and important precautions.

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

Trace the history of Linux from Unix and the GNU Project through the first Linux kernel, distributions, enterprise adoption, cloud computing, containers, and modern infrastructure.

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.

Linux touch Command: Create Files and Change Timestamps

Linux touch Command: Create Files and Change Timestamps

Learn how to use the Linux touch command to create empty files and change access or modification timestamps, including reference times, date formats, and symlink handling.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.

Linux ls Command: List Files and Directories

Linux ls Command: List Files and Directories

Learn how to use the Linux ls command to list files and directories with permissions, sizes, timestamps, and sorting options, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cut Command: Extract Characters and Fields

Linux cut Command: Extract Characters and Fields

Learn how to extract Characters and Fields with the Linux cut command, including practical examples, key options, and important precautions.

Linux resolvectl Command: Inspect systemd DNS Resolution

Linux resolvectl Command: Inspect systemd DNS Resolution

Learn how to inspect systemd DNS Resolution with the Linux resolvectl command, including practical examples, key options, and important precautions.

Linux basename Command: Extract the Final Component of a Path

Linux basename Command: Extract the Final Component of a Path

Learn how to use the Linux basename command to remove directory prefixes and optional suffixes from path strings, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux unxz Command: Decompress xz Files

Linux unxz Command: Decompress xz Files

Learn how to decompress xz Files with the Linux unxz command, including practical examples, key options, and important precautions.