How to transfer files between hosts with Secure Copy utility (SCP)

Time 2 min read

Updated 


Using the Secure Copy utility in Linux lets you securely transfer files to and from remote hosts via the SSH protocol, it comes with every Linux distribution, and it's very easy to use.

When using it, you will be prompted for the remote user’s password, or you can use an authorized SSH key if you have previously authorized it.

SCP Command Syntax

scp -OPTION user@SourceIP:file.txt user@DestinationIP:file.txt

When go through the options/flags that you can pass with the command:

  • -r: Recursively copy entire directories, also follows symbolic links
  • -P: Specifies an SSH port on destination host, use if destination uses any SSH port other than 22
  • -p: Copied files keep modification times, access times, permissions and modes
  • -v: Verbose mode; prints additional debugging messages about transfer progress
  • -q: Quiet mode; disables the progress bar and warning/diagnostic messages
  • -C: Compresses sent data for faster transfer speeds (useful for large files)

Copying files and directories to a remote host

Copy a file from local host to a remote host:

scp file.txt [email protected]:/directory

If preferred, use hostnames instead of IP addresses:

scp file.txt remote-user@hostname:/directory

You can also copy a file with a different name at the destination:

scp file.txt [email protected]:/directory/newfilename.txt

If the remote host uses a port besides the default 22 for SSH, specify it with the -P option:

scp -P 2222 file.txt [email protected]:/directory

To copy a directory, you'll need to do so recursively with the -r option:
scp -r /directory [email protected]:/directory

Copying from a remote host to the local machine

To copy from the local host rather than to it, put the remote host’s info first and the local host info after:

scp [email protected]:/directory/file.txt /local-directory

Transfer files between two remote hosts

With SCP its even possible to transfer between two other systems, as long as your local machine has access to both via SSH or password.

scp [email protected]:/directory/file.txt [email protected]:/directory/file.txt

Alternatively, use Rsync for large transfers

SCP is great for single files, or several small files, but when you’re dealing with many files or especially multiple directories with multiple sub-directories and files, a better option is Rsync.

References


Webmentions

Loading mentions...