In an environment with multiple Linux servers, you will often need to move files from one server to another. According to the number of files you have to move, there are different commands that can help you… In this guide we will assume that our servers are called alice and hatter, and our user on alice is rabbit, and on hatter mickey.
Steps
Step 1. For a single file, try the "scp" command
You can use it as a "push" or "pull" command, but let's start pushing the file to the other server. On Alice, use the command "scp myfile mickey @ hatter: quelfile". This command will copy the file to the other system, with the userid mickey mouse, and the name "quelfile". If you are logged into the other system, you could just as easily "pull" the file with the command "scp rabbit @ alice: myfile quelfile", and get the same result.
Step 2. To copy an entire folder, we can use the "scp" command again
This time we will add the -r switch, to make the copy action recursive. "scp -r my folder mickey mouse @ hatter:." will copy the entire "myfolder" folder to the other system, including all its contents and subfolders. The hatter folder will always have the name myfolder.
Step 3. What if you want to copy a lot of "messy" files and folders instead?
You could use the "tar" command to create a single file, and then copy it with the previous method, then use tar again to expand it on the other server. But this is not a Unix-style method. There must be a way to do it in one step, right? And so it is! Open your favorite shell. We can still use tar to compact the files we want to move, and then use ssh to transfer them to the other system (the method used by scp), and tar on the second server to expand them. But why waste time and space to create a real tar file, when we could simply create a pipe between the two systems to transfer the tar data to? Using the same folder as the previous example, try "tar -cf - my folder / * | ssh mickey @ hatter 'tar -xf -'"
Advice
- You should replace usernames, hostnames, filenames, folder names according to your network configuration when using the above commands. The commands shown above are just examples of commands useful for copying files between servers.
- Of course, there are many other ways to accomplish the same thing. Linux offers a lot of tools..