SCP Command in Linux: How to Use It, with Examples

SCP Command in Linux

The SCP command in Linux allows you to copy files over ssh connections. This is pretty useful if you want to transport files between computers.

The scp (secure copy) command uses SSH to transfer data from one host to another, and uses the same authentication and security provided by SSH. The command relies on SSH for data transfer, so it requires an ssh key or password to authenticate on the remote systems.

When transferring data with scp, both the files and password are encrypted so that anyone snooping on the traffic doesn’t get anything sensitive. As such, this is one of the most secure ways to transfer data on a network.

The scp command in Linux can be used in 3 ways:

  • To copy from a remote server to local machine.
  • Also, to copy from local machine to a remote server.
  • To copy from a remote server to another remote server.

SCP Command Syntax

The syntax for the scp command is:

scp [OPTION] [[email protected]]SRC_HOST:]file1 [[email protected]]DEST_HOST:]file2

Some of the most widely used scp command options include:

  • -P – Specify server SSH port
  • -p – Preserves permissions, modes and access time of files (note the lower-case)
  • -q – Quiet mode, don’t display progress or messages
  • -C – Compress the data during transmission
  • -r – Recursive – include subdirectories and their contents
  • -i – Identity file or private key

How to Use the SCP Command in Linux

Transfer Local File to Remote Server

Copy file.txt from current directory of local system to remote server’s /tmp directory.

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

Transfer File from Remote Server to Local Machine

Following command will copy /tmp/file.txt from remote server to local machine under the user’s home directory.

scp [email protected]:/tmp/file.txt /home/user

Transfer Local Directory to Remote Server Recursively

Use -r option in scp command in Linux to recursively copy the entire directory from one system to another.

Following command will copy /home/user/myfiles directory from local machine to remote server’s /tmp directory recursively.

scp -r /home/user/myfiles [email protected]:/tmp/

Transfer Directory from Remote Server to Local Recursively

Following command will copy /tmp/serverfiles directory from remote server to local machine under the user’s home directory recursively.

scp -r [email protected]:/tmp/serverfiles /home/user

Transfer Multiple Files to Remote Server

In the following example, the files file1.txt and file2.txt from the source host are copied to the remote server’s /tmp directory.

scp file1.txt file2.txt [email protected]:/tmp/

Increase Transfer Speed by Enabling Compression

You can increase the transfer speed by enabling the compression using -C option. It will automatically enable compression at source and decompression at destination host.

Following command will copy /home/user/myfiles directory from local machine to remote server’s /tmp directory recursively with enabled compression.

scp -r -C /home/user/myfiles [email protected]:/tmp/

Specify Different SSH Port

There can be some cases where SSH port is changed on destination host, so while using scp command in Linux you can specify the SSH port number using -P option.

Following command will copy file.txt from current directory of local system to remote server’s /tmp directory using port 2222.

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

Preserves permissions, modes and access time of files

Use -p option in scp command to preserve permissions, access time and modes while copying files.

Following command will copy file.txt from current directory of local system to remote server’s /tmp directory and will keep its properties.

scp -p file.txt [email protected]:/tmp/

Use Identify File in SCP Command

When using an SSH Key instead of a password during the SSH session, the -i flag allows you to selects the file from which the identity (private key) for public key authentication is read.

Following command will copy file.txt from current directory of local system to remote server’s /tmp directory using the my_second_indent.pem private key file.

scp -i my_second_indent.pem file.txt [email protected]:/tmp/

For more about scp command in Linux, consult its manual page.

Leave a Reply

Your email address will not be published.