Skip to main content

scp command

Detailed Explanation

scp (secure copy) copies files between hosts on a network. It uses SSH for data transfer, and uses the same authentication and provides the same security as SSH. It will ask for passwords or passphrases if they are needed for authentication.

Basic Syntax

scp [options] [[user@]host1:]file1 ... [[user@]host2:]file2

Common Flags/Options

OptionDescription
-rRecursively copy entire directories.
-P <port>Specifies the port to connect to on the remote host. (Note: capital 'P' unlike ssh).
-i <identity_file>Selects the file from which the identity (private key) for public key authentication is read.
-CEnable compression. Passes the -C flag to ssh to enable compression.
-vVerbose mode.

Real-world Examples

Copy a local file to a remote server:

scp file.txt user@192.168.1.100:/path/to/destination/

Copy a remote file to the local machine:

scp user@192.168.1.100:/remote/path/file.txt /local/path/

Copy a directory recursively to a remote server:

scp -r /local/directory/ user@example.com:/remote/directory/

Copy a file using a specific SSH port and private key:

scp -P 2222 -i ~/.ssh/my_key file.txt user@example.com:/tmp/