# Cheatsheet & Examples: scp

`scp` (secure copy) is a command-line utility used to securely transfer files and directories between a local host and a remote host or between two remote hosts. It uses the Secure Shell (SSH) protocol for secure data transfer, encrypting the data during transmission.

## Copying a file from local machine to a remote machine

Example Usage:
`scp /path/to/local/file username@remote_host:/path/to/remote/directory/`

What it does:
Copies a local file to a specified directory on a remote server.

Command-line Arguments Explained:

- `/path/to/local/file`: The local path to the file you want to copy.
- `username@remote_host`:  Specifies the remote user and host. The format is `username@hostname` or `username@IP_address`. You will be prompted for the user's password on the remote host.
- `/path/to/remote/directory/`: The destination directory on the remote host. If the directory doesn't exist, `scp` won't create it, and the transfer will fail.  The filename in the destination will be the same as the original. If you want a different filename on the remote end, specify the full path including the new filename: `/path/to/remote/directory/new_filename`.

## Copying a file from a remote machine to the local machine

Example Usage:
`scp username@remote_host:/path/to/remote/file /path/to/local/directory/`

What it does:
Copies a file from a specified location on a remote server to a local directory.

Command-line Arguments Explained:

- `username@remote_host`: Specifies the remote user and host. The format is `username@hostname` or `username@IP_address`. You will be prompted for the user's password on the remote host.
- `/path/to/remote/file`: The path to the file on the remote server you want to copy.
- `/path/to/local/directory/`: The local destination directory. The filename in the destination will be the same as the original. If you want a different filename, specify the full path including the new filename: `/path/to/local/directory/new_filename`.

## Copying a directory from local machine to a remote machine

Example Usage:
`scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory/`

What it does:
Copies a directory and all its contents (recursively) from your local machine to a remote server.

Command-line Arguments Explained:

- `-r`: The recursive option. This tells `scp` to copy directories and their contents.
- `/path/to/local/directory`: The local path to the directory you want to copy.
- `username@remote_host`: Specifies the remote user and host. You will be prompted for the user's password on the remote host.
- `/path/to/remote/directory/`: The destination directory on the remote host. The directory structure will be preserved. If the destination directory does not exist, it will be created.

## Copying a directory from a remote machine to the local machine

Example Usage:
`scp -r username@remote_host:/path/to/remote/directory /path/to/local/directory/`

What it does:
Copies a directory and all its contents (recursively) from a remote server to your local machine.

Command-line Arguments Explained:

- `-r`: The recursive option. This tells `scp` to copy directories and their contents.
- `username@remote_host`: Specifies the remote user and host. You will be prompted for the user's password on the remote host.
- `/path/to/remote/directory`: The path to the directory on the remote server you want to copy.
- `/path/to/local/directory/`: The local destination directory. The directory structure will be preserved. If the destination directory does not exist, it will be created.

## Specifying a custom SSH port

Example Usage:
`scp -P 2222 username@remote_host:/path/to/remote/file /path/to/local/directory/`

What it does:
Copies a file from a remote host to the local machine, specifying a non-standard SSH port.

Command-line Arguments Explained:

- `-P 2222`: Specifies the port number to use for the SSH connection. In this example, it uses port 2222.
- `username@remote_host`: Specifies the remote user and host. You will be prompted for the user's password on the remote host.
- `/path/to/remote/file`: The path to the file on the remote server you want to copy.
- `/path/to/local/directory/`: The local destination directory. The filename in the destination will be the same as the original.

## Using a private key for authentication

Example Usage:
`scp -i /path/to/private/key username@remote_host:/path/to/remote/file /path/to/local/directory/`

What it does:
Copies a file from a remote host to the local machine, authenticating using a private key file.

Command-line Arguments Explained:

- `-i /path/to/private/key`: Specifies the identity file (private key) to use for authentication.
- `username@remote_host`: Specifies the remote user and host. No password prompt should appear if the key is configured correctly on the remote host.
- `/path/to/remote/file`: The path to the file on the remote server you want to copy.
- `/path/to/local/directory/`: The local destination directory. The filename in the destination will be the same as the original.

## Copying multiple files

Example Usage:
`scp file1.txt file2.txt username@remote_host:/path/to/remote/directory/`

What it does:
Copies multiple files from the local machine to a remote directory.

Command-line Arguments Explained:

- `file1.txt file2.txt`: List of the files to be copied.
- `username@remote_host`: Specifies the remote user and host. You will be prompted for the user's password on the remote host.
- `/path/to/remote/directory/`: The destination directory on the remote host. All files will be copied to this directory. The filenames will remain the same.

## Limiting bandwidth usage

Example Usage:
`scp -l 1024 /path/to/local/file username@remote_host:/path/to/remote/directory/`

What it does:
Copies a local file to a remote machine while limiting the bandwidth used.

Command-line Arguments Explained:

- `-l 1024`: Specifies the bandwidth limit in kilobits per second. In this example, it's set to 1024 kbps (1 Mbps).
- `/path/to/local/file`: The local path to the file you want to copy.
- `username@remote_host`: Specifies the remote user and host. You will be prompted for the user's password on the remote host.
- `/path/to/remote/directory/`: The destination directory on the remote host. The filename will remain the same.

## Preserve modification times and access times

Example Usage:
`scp -p /path/to/local/file username@remote_host:/path/to/remote/directory/`

What it does:
Copies a local file to a remote machine, preserving the original modification and access times of the file.

Command-line Arguments Explained:

- `-p`: Preserves modification times, access times, and permissions of the file(s).
- `/path/to/local/file`: The local path to the file you want to copy.
- `username@remote_host`: Specifies the remote user and host. You will be prompted for the user's password on the remote host.
- `/path/to/remote/directory/`: The destination directory on the remote host. The filename will remain the same.
