SCP – Copying Files Between Servers – Assistance, Tips, and Examples

Certainly! SCP (Secure Copy Protocol) is a command-line tool used to securely copy files between servers over SSH. Here’s a guide with help, tips, and examples for using SCP:

Basic SCP Syntax:

  • Copy a file from local to remote:
    scp local_file.txt username@remote_server:/path/to/destination/
  • Copy a file from remote to local:
    scp username@remote_server:/path/to/remote_file.txt /local/destination/

SCP with Specific Port:

  • Specify a port other than the default (22):
    scp -P 2222 local_file.txt username@remote_server:/path/to/destination/

SCP Tips:

  1. Copy Entire Directories:
    • Use the -r flag to recursively copy directories:
      scp -r local_directory/ username@remote_server:/path/to/destination/
  2. Verbose Mode:
    • Use the -v flag for verbose output to see details of the transfer:
      scp -v local_file.txt username@remote_server:/path/to/destination/
  3. Preserve File Attributes:
    • Preserve timestamps and permissions with the -p flag:
      scp -p local_file.txt username@remote_server:/path/to/destination/
  4. Quiet Mode:
    • Suppress progress meter and non-error messages with the -q flag:

      scp -q local_file.txt username@remote_server:/path/to/destination/

Examples:

  • Copy a file from local to remote with a specific port:
    scp -P 2222 local_file.txt username@remote_server:/path/to/destination/
  • Copy a directory from local to remote:
    scp -r local_directory/ username@remote_server:/path/to/destination/
  • Copy a file from remote to local with verbose output:
    scp -v username@remote_server:/path/to/remote_file.txt /local/destination/
  • Copy a file preserving timestamps and permissions:
    scp -p local_file.txt username@remote_server:/path/to/destination/

Using SSH Key for Authentication:

  • Specify the private key file with -i:

    scp -i /path/to/private_key.pem local_file.txt username@remote_server:/path/to/destination/

Interactive Mode:

  • Use interactive mode for multiple files:
    scp -r username@remote_server:/path/to/remote_directory/* /local/destination/

Copying Between Remote Servers:

  • Copy from one remote server to another:
    scp username@remote_server1:/path/to/remote_file.txt username@remote_server2:/path/to/destination/

These examples cover some common use cases of SCP. Remember to replace placeholders like local_file.txt, remote_server, etc., with your actual file names and server details. Always ensure that you have the necessary permissions and connectivity for the file transfer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.