Copying Files between Clients and Servers over ssh using scp

It’s quite common to need to upload or download file between one or more servers and the local computer.

If it’s available a ssh access on the servers, using scp to transfer file from and to the server could be a very good option.

Here’s its syntax:

usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 ... [[user@]host2:]file2

A very simple example:

scp localfilename.txt remoteuser@www.remotehost.com:remotefilename.txt

The previous example copies localfilename.txt from the local directory to the server www.remotehost.com using remoteuser as the ssh account to authenticate on the remote server. On the remote server the transferred file will be stored as remotefilename.txt in the default login directory of remoteuser.

Copying file from and to specific directories:

scp /localdir/localfilename.txt remoteuser@www.remotehost.com:/remotedir/remotefilename.txt

Compared to the previous example, in this case, the file is taken from /localdir/localfilename.txt and stored remotely on /remotedir/remotefilename.txt.
Obviously remoteuser should have write permission on the remote directory where the file is going to be written.

In the next case, the authentication is made through a keyfile, this is the syntax:

scp -i keyfile /localdir/localfilename.txt remoteuser@www.remotehost.com:/remotedir/remotefilename.txt

In this case to login as remoteuser there will not be a prompt for password, but keyfile is used as identity file.

It’s even possible to copy directly files from one server to another

scp firstremoteuser@www.firstserver.com:/filename.txt anotherremoteuser@www.anotherserver.com:/remotedir/remotefilename.txt

Finally one of the best features is to copy recursively directory trees to the remote server:

scp -r /localdirectory remoteuser@www.remoteserver.com:/remotedirectory

In this case, the whole content of localdirectory is recursively copied into remotedirectory. This can be very useful for moving quickly website structures.
I hope you’ve found some useful information in this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>