Featured image of post SCP Command Syntax for Copying Files and Directories

SCP Command Syntax for Copying Files and Directories

The SCP command is used for securely copying files and directories between Linux and Unix operating systems. It employs the SSH protocol to ensure...

The scp command is used to securely copy files and directories between Linux and Unix operating systems. It employs the SSH protocol to encrypt and authenticate data transfers, ensuring the security and integrity of the data. Windows 10 and later versions also include this command, making it easy to transfer large files from Windows to Linux systems.

SCP Command Syntax for Copying Files and Directories

The syntax for the scp command is as follows:

1
scp [options] [source file or directory] [destination file or directory]

Here, [options] is optional, and the following parameters can be used with the scp command:

  • -P: Specify the SSH port number.
  • -r: Recursively copy an entire directory.
  • -v: Enable verbose mode to display the progress of the scp command.
  • -C: Enable compression to reduce the size of the data being transferred.
  • -p: Preserve file modification times, access times, and permissions.

[source file or directory] specifies the local path of the file or directory to be copied. If you want to copy multiple files, you can use wildcards to match the relevant files.

[destination file or directory] specifies the path on the target host (which can be a local or remote path) in the format [user@]host:[/path/to/]file. If the user is omitted, the current user is used by default. If the remote path is omitted, the default is the current user’s home directory.

Here are some examples of using the scp command:

  1. Copying a file from local to remote server:
1
scp /path/to/local/file.txt user@remote:/path/to/remote/file.txt
  1. Copying a file from remote server to local:
1
scp user@remote:/path/to/remote/file.txt /path/to/local/file.txt
  1. Copying an entire directory from local to remote server:
1
scp -r /path/to/local/directory user@remote:/path/to/remote/directory
  1. Copying an entire directory from remote server to local:
1
scp -r user@remote:/path/to/remote/directory /path/to/local/directory

Note that the scp command requires an established SSH connection before execution. If you have set up SSH key authentication between the local and remote hosts, you can avoid entering a password each time. For more information, please refer to the relevant documentation or type man scp in the terminal to view the manual page for the scp command.

Licensed under CC BY-NC-SA 4.0