how to transfer files over network using scp

Secure Copy or scp is a command line utility that allows you to copy files and folders between two locations. These locations can be on the same machine, on different disk drives on the same machine or even on remote machines. The utility takes care of the security and permissions while transferring the files which makes it easier to copy files.

scp vs cp

If you have been using Linux or any of its posix variants then it is very likely that you have come across the file copying utility named cp. The cp is the utility that allows one to copy files from one location to another. While this works well with local directories, it is not the best option when trying to move or copy between the machines or over the network.

The scp or Secure Copy is the linux utility that allows you to copy files and folders across the network with the same ease of the cp command. The scp needs the ssh to be installed on the machines which probably is the case with almost all versions of Linux. You would need to have a operational ssh server on your target machine and a ssh client on your source machine.

The scp uses the RCP (Remote Copy) for transferring files and uses the SSH protocol for security, encryption and authentication. So the use of SSH as the underlying protocol provide many of the benefits to keep the date safe and secure.

You can use the scp client to copy files to and from the ssh servers. The scp command works very similar to the cp command with similar command line syntax. The command have both a source as well as a target (or destination) along with some command line options as needed. However, at least one of the location will be a remote location…Yes. It is quite possible to have both the source and target locations to be remote.

Command Syntax

As I mentioned earlier, the scp has a similar syntax to the cp command. This should potentially make it easier for it to be used. It takes two command line arguments: the first is the source and the second one is the target. Either or both of the source and the target may be a local pathname or a remote pathname. The path specified for a remote host is usually in one of the two formats.

[user@]host[:path]

scp://[user@]host[:port][/path]

In addition to the two file arguments there are a lot of command line options that lets you specify various options to be used while transferring files. So a typical scp command will look like this…

$ scp -v ./myfile.txt userOne@remotemc:/home/userOne/myfile.txt

Security and Authentication

It is quite possible you will need to provide authentication if you are reading or writing data to a different machine. If additional authentication in needed, then the utility will prompt you to provide the required input such as password.

It is possible to provide authentication details so that you are not prompted. You can use SSL certificates or provide the password in the command line as plain text.

Examples

We will use some common examples that illustrate the use of the command in many cases. Your case may be different, and the man (manual page for scp) will be the best resource.

To transfer a file from the local machine to a remote machine, you will use the following command.

$ scp -v ./myfolder/myfile.txt tom@remote:/home/tom/myfolder/myfile.txt

Just like the cp command you can use wild cards to specify multiple files or a set of files. So to transfer a set of files from a remote machine to the local machine, you could use something like:

$ scp -v tom@remote:/home/tom/myfolder/*.txt ./myfolder/

You can also copy entire folder structure. The recursive copy is specified using the command line option -r which will recursively copy entire directories. In order to copy folder structure or the folders recursively over the network to a remote machine named remote ….

$ scp -rv tom@remote:/home/tom/myfolder .

There are a lot of command line options that allow you to pretty much control all of the transfer properties. A detailed explanation of all of those is probably outside the scope of this post.