SCP Command with Examples
Securely Transfer Files
The scp command is used to securely copy files and directories between a local host and a remote host using the SSH (Secure Shell) protocol. It provides a secure way to transfer data over a network, similar to the cp command for local file copying. Here's the basic syntax and usage of the scp command:
scp [options] source destination
source: The file or directory you want to copy from the local host.destination: The location on the remote host where you want to copy the file or directory.
Examples:
- Copy a local file to a remote server:
scp local-file.txt username@remote-server:/path/to/remote/directory/
- Copy a remote file to the local machine:
scp username@remote-server:/path/to/remote-file.txt /local/directory/
- Copy a directory and its contents recursively to a remote server:
scp -r local-directory/ username@remote-server:/path/to/remote/directory/
- Copy a file from a remote server to another remote server (through your local machine):
scp username@source-remote:/path/to/source-file.txt username@destination-remote:/path/to/destination-directory/
Common Options:
-r: Recursively copy directories and their contents.-P port: Specify a custom SSH port (default is 22).-i identity_file: Specify a private key file for authentication.-C: Enable compression during data transfer.-v: Verbose mode for detailed output (use-vvor-vvvfor even more verbosity).
Note: The scp command requires SSH access to the remote host. Make sure you have the necessary permissions and credentials to access the remote server.
Example with Options:
scp -r -P 2222 -i ~/.ssh/private_key.pem local-dir/ username@remote-server:/path/to/remote/directory/
This command copies the local-dir directory and its contents to the remote server using port 2222 for SSH and the specified private key for authentication.
Remember to replace placeholders like local-file.txt, username, remote-server, etc., with actual values relevant to your scenario.