How to use the scp command in Linux

fatmawati achmad zaenuri/

The scp The command makes it easy and secure to copy files between Linux computers. It uses SSH security, but most importantly, it’s simple. If you can use cpyou can use scp.

The secure copy protocol and scp

Let’s define a few terms: there is SCP and there is scp. The capital SCP represents the Secure Copy Protocol. The tiny scp stands for security cp. In other words, SCP is a protocol and scp is a program.

scp was designed to be a safe and secure way to copy files between remote Linux computers. It uses SSH to establish secure connections. SSH, or secure shell, is a cryptographic network protocol often used to access and connect to remote Linux computers. On Linux distributions, SSH functionality is provided by OpenSSH.

SCP is a bit long in the tooth, and concerns have been raised about its use these days. As of OpenSSH version 8.8, SCP is considered deprecated. Modern implementations of scp default using the default secure file transfer protocol. SSH is still used for the secure connection, but file transfers are handled by SFTP. All of this is invisible and happens like magic under the hood, and the scp the syntax remained the same.

The rsync program is preferred to scp but you may come across a computer that does not have rsync installed, and for which you don’t have root privileges, which means you can’t go ahead and install it. To copy files from one computer to another on a stand-alone network, scp is perfectly fine. For scp to work you need to have SSH running on all the computers you will be copying to and from.

To see the version of OpenSSH installed on your computer, type:

ssh -V

Get OpenSSH version

Single File Copy

As the standard cp ordered, scp copy files from The source location at the target location. To copy a file to a remote computer, you need to know the remote computer’s IP address or network name. You must also have credentials for a user account that has write privileges for the location you are sending the file to.

To send a file called “sample.txt” to a computer called “fedora-34” on the local network, the syntax is:

scp ./sample.txt [email protected]:/home/dave/Downloads/

Copy a single file to a remote computer

The order is composed of:

  • scps: The scp command
  • ./example.txt: The file we are going to send. It’s in the current directory.
  • [email protected]: The user account on the remote computer to which we will send the file.
  • fedora-34.local: network name of the remote computer.
  • :/home/dave/Downloads/: The location to copy the file to on the remote computer. Note the colon “:” that separates the computer name and the path.

You will be prompted to enter the password for the account on the remote computer, then the file will be copied.

If you want the file to have a different name on the remote computer, you can add a filename to the target path. To copy the same file and name it “different-file.txt”, use this syntax:

scp ./sample.txt [email protected]:/home/dave/Downloads/different-file.txt

Copying a single file to a remote computer with a new name

The scp The command will silently overwrite existing files, so be careful when copying files. If a file already exists on the target computer with the same name as the file you are copying, it will be overwritten and lost.

If the target computer is not using the default SSH port of 22, you can use the -P (port number) to provide the appropriate port number.

Single file recovery

To copy a file of a remote server, just put the remote server as the source and put the local path where you want the file to be copied as the target. We will copy a file called “” from the remote computer to the current directory on the local computer.

scp [email protected]:/home/dave/Downloads/ .

Copying a single file from a remote server to the local computer's current directory

If you add a filename to the local path, the file is copied and given that name.

scp [email protected]:/home/dave/Downloads/ ./

Copying a single file from a remote server to the local computer's current directory with a new name

The file is copied but renamed to our specified filename.

ls -hl *.md

Copy multiple files

Copying multiple files back and forth is easy. You can list as many source files as you want. Here we copy two Markdown files and a CSV file.

scp ./ ./ ./dp-3.csv [email protected]:/home/dave/Downloads/

Copy multiple named files to a remote computer

The three files are copied to the remote computer. You can also use wildcards. This command does exactly the same as the last command.

scp ./dp. [email protected]:/home/dave/Downloads/

Copying multiple files to a remote computer using wildcards in the filename

Recursive copying of directories

The -r (recursive) allows you to copy entire directory trees with a single command. We placed two files in a directory called “data” and created a directory called “CSV” inside the “data” directory. We placed a CSV file in the “data/CSV” directory.

This command copies the files and recreates the directory structure on the remote computer.

scp -r ./data [email protected]:/home/dave/Downloads/

Copying a directory tree to a remote computer

Copy files between remote servers

You can even educate scp to copy files from one remote server to another. The syntax is quite simple. You provide the account name and network address of the source server and the account name and network address of the target server. Files are copied from the source server and copied to the location on the target server.

Although the syntax is simple, making sure everything else is in place takes a bit more thought. Obviously, the location you are trying to copy the files to on the remote server must be accessible by the user account you specify on the command line. And that user account must have write permissions to that location.

A more subtle prerequisite is that SSH access must be configured between your local computer and the source computer, as well as between the source and target servers. Make sure you can use SSH to connect to the target server from source server. If you can’t do that, scp will not be able to log in.

Setting up SSH keys so you can use authenticated but passwordless access is by far the preferred method. Using passwords gets messy very quickly, and since you’re prompted to enter the password for each user account, it prevents you from fully automating the process with a script.

We configure SSH keys for the user accounts we use on each remote server. This provided transparent SSH access to the other server, for these two users. This allows us to transfer files back and forth, using these two user accounts.

To copy files from the “davem” user account on a Manjaro computer to the “dave” account on a Fedora computer, via a scp command issued from our local Ubuntu computer, the syntax is:

scp [email protected]:/home/davem/man. [email protected]:/home/dave/

Copy files from one remote server to another.

We silently return to the command line. There is no indication that anything happened. Assuming that no news is good news, scp only reports errors for this remote-to-remote copy. By checking the Fedora computer, we can see that the files from the Manjaro computer have been copied and received.

Files from Manjaro computer received on Fedora computer

By default, files are copied directly from the source computer to the target computer. You can override this using the -3 (three ways).

With this option, files are transferred from the target to the source, via your local computer. For this to happen, there must be transparent SSH access from your local computer to the target computer.

scp -3 [email protected]:/home/davem/man. [email protected]:/home/dave/

Copy files from one remote server to another, via the local computer

There is still no indication that anything happened, even when transmitting the files through your local computer. The proof of the pudding, of course, is to verify the target computer.

Other Options

The -p (preserve file attributes) will retain the original file’s creation, ownership, and access flags on transferred files. They will have the same metadata as the original files on the source computer.

If you see any error messages, try repeating the command and use the -v (verbose) to display detailed information about the transfer attempt. You should be able to spot the point of failure in the output.

The -C The (compress) option compresses files when they are copied and decompresses them when they are received. This is something that dates back to the era of slow modem communications between computers. Reducing the payload size could reduce transmission times.

Nowadays, the time required to compress and decompress files is likely to take longer than the difference between compressed and uncompressed transmissions. But because scp is best used for copying files between computers on the same local network, transmission speed shouldn’t be an issue.

RELATED: How to backup your Linux system with rsync

Previous Longtime Waffle House patrons raise thousands for medical bills for beloved waiter with cancer - WSB-TV Channel 2
Next On World Environment Day, Guaranteed Rate Announces Price FlashClose[SM] Fintech Tool saved more than 17 million sheets of paper