How to Transfer Files from One Linux Server to Another

For a single file, try the "scp" command., To copy an entire directory, we can again turn to the "scp" command., What if you have a large "mess" of files and directories to copy?

5 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: For a single file

    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. Step 2: try 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. , 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
    -'"
  3. Step 3: To copy an entire directory

  4. Step 4: we can again turn to the "scp" command.

  5. Step 5: What if you have a large "mess" of files and directories to copy?

Detailed Guide

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.

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. , 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
-'"

About the Author

C

Cynthia Allen

Professional writer focused on creating easy-to-follow hobbies tutorials.

100 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: