Linux scp Command

Secure Copy Protocol - 10 Practical Examples with Detailed Explanations

Example 1

Copy File from Local to Remote Server

$ scp file.txt user@remote_host:/path/to/destination/
file.txt 100% 1024KB 1.0MB/s 00:01
Copies a local file to a remote server using SSH protocol. Replace 'user' with your username, 'remote_host' with the server address (IP or hostname), and specify the destination path on the remote server.
Syntax: scp source_file user@host:destination_path
Example 2

Copy File from Remote Server to Local

$ scp user@remote_host:/path/to/file.txt /local/destination/
file.txt 100% 2048KB 2.0MB/s 00:01
Downloads a file from a remote server to your local machine. This is the reverse operation of Example 1. The remote path comes first, followed by the local destination.
Tip: Use '.' as the destination to copy to current directory: scp user@host:file.txt .
Example 3

Copy Directory Recursively

$ scp -r /local/directory user@remote_host:/remote/destination/
file1.txt 100% 512KB 512.0KB/s 00:01
file2.txt 100% 256KB 256.0KB/s 00:01
subdirectory/file3.txt 100% 128KB 128.0KB/s 00:01
The '-r' option enables recursive copying of entire directories and their contents, including subdirectories. Essential for transferring complete directory structures between systems.
Important: Directory permissions and timestamps are preserved during recursive copy
Example 4

Copy with Custom SSH Port

$ scp -P 2222 file.txt user@remote_host:/destination/
Uses the '-P' option (uppercase) to specify a custom SSH port. Default SSH port is 22, but many servers use different ports for security. Always use uppercase -P for scp (lowercase -p is for preserving file attributes).
Security: Custom ports are common security practice to reduce automated attacks
Example 5

Preserve File Attributes

$ scp -p file.txt user@remote_host:/destination/
The '-p' option (lowercase) preserves modification times, access times, and file modes from the original file. Useful when you need to maintain exact file metadata during transfer.
Remember: -P (uppercase) = Port, -p (lowercase) = Preserve attributes
Example 6

Copy Multiple Files

$ scp file1.txt file2.txt file3.txt user@remote_host:/destination/
file1.txt 100% 1024KB 1.0MB/s 00:01
file2.txt 100% 2048KB 2.0MB/s 00:01
file3.txt 100% 512KB 512.0KB/s 00:01
Transfers multiple files in a single command. List all source files separated by spaces before specifying the destination. More efficient than running scp multiple times.
Wildcards: Use patterns like *.txt to copy all matching files
Example 7

Copy Between Two Remote Hosts

$ scp user1@host1:/path/to/file.txt user2@host2:/destination/
Transfers files directly between two remote servers without downloading to your local machine first. Your local machine acts as a bridge for authentication, but the data flows directly between the servers.
Requirement: You must have SSH access to both remote hosts
Example 8

Use SSH Key Authentication

$ scp -i ~/.ssh/id_rsa file.txt user@remote_host:/destination/
Specifies a private key file for authentication using the '-i' option. This is essential when using key-based authentication instead of passwords, or when you have multiple SSH keys.
Best Practice: Key-based authentication is more secure than password authentication
Example 9

Limit Bandwidth Usage

$ scp -l 1024 largefile.tar.gz user@remote_host:/destination/
The '-l' option limits bandwidth in Kbit/s. This example limits transfer to 1024 Kbit/s (128 KB/s). Useful for preventing scp from consuming all available bandwidth, especially when transferring large files.
Note: Bandwidth is specified in Kilobits per second, not Kilobytes
Example 10

Verbose Mode with Compression

$ scp -Cv file.txt user@remote_host:/destination/
Executing: program /usr/bin/ssh host remote_host
OpenSSH_8.2p1, OpenSSL 1.1.1
debug1: Reading configuration data /etc/ssh/ssh_config
Sending file modes: C0644 1024 file.txt
file.txt 100% 1024KB 1.5MB/s 00:01
Combines '-C' (compression) and '-v' (verbose) options. Compression reduces transfer time for large text files, while verbose mode shows detailed progress and debugging information. Particularly useful for troubleshooting connection issues.
Performance: Compression helps with text files but may slow down already-compressed files (zip, jpg, mp4)