Transfer Files from One Linux Server to Another

In a multi-server Linux environment, many tasks involve moving one or more files from one server to another. Depending on the number of files you need to move, there are several commands that may help you out....

Let's assume for these discussions that our servers are Alice and Madhat, and that our user on Alice is rabbit, and our user on madhat is Fieldmouse.

Steps

  1. For a single file, try the "scp" command. You can use this as a "push" or a "pull" command, but let's start with pushing the file to the other server. While on alice, use the command "scp myfile fieldmouse@madhat:thatfile". This will copy the file over to the other system, into the fieldmouse userid, with the name "thatfile". If you were logged in on the other system, you could just as easily pull the file with the command "scp rabbit@alice:myfile thatfile", and get the same results.
  2. To copy an entire directory, we can again turn to the "scp" command. This time we'll add the -r switch, to cause the copy to act "recursively". "scp -r mydir fieldmouse@madhat:." will copy the entire directory "mydir" over to the other system, including all its contents and additional directories. The directory on madhat will still be named mydir.
  3. What if you have a large "mess" of files and directories to copy? You could use the "tar" command to create a single file, and then copy that file as above, then use tar to expand it on the other server... But that would seem so... Un-unix-like. There has to be a way to do it in a single step, right? Well of course!

    Enter your favorite shell's pipes. We can still use tar to package up the files we want, and then use ssh to get it to the other system (which is what scp has been using under the covers), and tar on the other side to expand the files back out. But why waste time and space creating an actual tar file, when we could just create a pipe that spans the two systems and transfer the tar data through it?

    Using the same directory as in the previous example, try "tar -cf - mydir/* | ssh fieldmouse@madhat 'tar -xf -'"

Tips

  • Of course there are other ways of doing this as well. Linux is full of tools. Your mileage may vary
  • You should replace user name/host name/file name/directory name according to your network configuration and environment while using above commands. Commands shown above are only examples of executing commands for copying files on server.

Warnings

  • Be sure that UIDs and GIDs on the various systems you're using match up (Not just the user names). If they don't, interesting security problems will ensue.

Related Articles