Copying files between 2 Linux servers without downloading it first in your PC

Posted by Darwin Biler on September 22, 2014

The title might sounds dumb, but many Web Developers who aren't really used into System Administration will fall into this trap. Let's say you are into this situation:

  • You have a Linux server (Server A) in Hosting Provider A
  • You have a Linux server (Server B) in Hosting Provider B
  • You have SSH access to Server A
  • You have FTP account to Server B
  • You need to transfer/copy a bunch of files from Server B to Server A
  • There is no way the Hosting Provider A can have a NFS mount to Hosting Provider B

The usual instinct of a usual user w/o  background (or doesn't have enough experience ) in Linux is to download the files from Server B to his PC, then manually upload it to Server A.

Though most of the time, you can get away with that, it becomes an issue when you got gigabytes of files to transfer, since that would mean you need to download the files for hours in your PC then spend hours as well in uploading it to the other server.

One of the easiest way to do that is:

1. Identify the FTP path of the folder you want to copy in Server B, lets say its on example.com/myfolder

2. In SSH, go to the folder where you want to save the files into

cd myfolder_in_server_A

3. execute the following:

wget -r -nH --cut-dirs=2 ftp://username:password@serverB/example.com/myfolder

replace the code above with the real values:
username - is your FTP username
password - is your FTP password
serverB - is the FTP hostname of Server B
example.com/myfolder - is the FTP path in Server B

The command above basically connects to serverB and copies the contents of example.com/myfolder into the current directory.

The 2 in the --cut-dirs parameter is the number of folders to "cut", in this example, example.com/myfolder is two folders, that is why it is two.
But lets say you have this path in the FTP server

ftp://username:password@serverB/example.com/myfolder/hello/darwin/this/is

Then the code should be

wget -r -nH --cut-dirs=6 ftp://username:password@serverB/example.com/myfolder/hello/darwin/this/is

The above situation can be one of the many situation on which you will be able to use this trick.


Did you find this useful?

I'm always happy to help! You can show your support and appreciation by Buying me a coffee (I love coffee!).