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 awk Command: Analyze Text by Fields

Linux awk Command: Analyze Text by Fields

Learn how to analyze Text by Fields with the Linux awk command, including practical examples, key options, and important precautions.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Essential Linux Commands: A Practical Reference by Task

Essential Linux Commands: A Practical Reference by Task

Browse essential Linux commands by task, including help, files, text processing, permissions, processes, networking, packages, storage, and system administration.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux free Command: Check Memory and Swap Usage

Linux free Command: Check Memory and Swap Usage

Learn how to use the Linux free command to inspect available memory, used memory, cache, and swap usage, 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 ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Learn how to generate SSH Keys and Inspect Fingerprints with the Linux ssh-keygen command, including practical examples, key options, and important precautions.

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Practice creating, copying, renaming, moving, and deleting files and directories in a dedicated folder while checking each result safely.

Linux ssh Command: Connect to a Remote Host Securely

Linux ssh Command: Connect to a Remote Host Securely

Learn how to connect to a Remote Host Securely with the Linux ssh command, including practical examples, key options, and important precautions.

Linux diff Command: Compare Text Files and Directories

Linux diff Command: Compare Text Files and Directories

Learn how Linux diff compares text files and directories, produces unified patches, ignores selected whitespace changes, and reports differences through exit status.