In a Linux multi-server 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 can help you …. Let's assume for these discussions that our servers are alice and madhat, and that our user at alice is rabbit and our user at madhat is fieldmouse.
Step 1. For a simple file, try the "scp" command
You can use this as a "push" or "pull" command, but let's start by pushing the file to another server. While on alice use the command "scp myfile fieldmouse @ madhat: thatfile". This will copy the file to the other system under the userid "thatfile". If you were logged into a different system, you can just as easily pull the file with the command "scp rabbit @ alice: myfile thatfile" and get the same result.
Step 2. To copy the entire directory, we can use the "scp" command again
This time we'll add the -r switch to force the copy to act "recursively". "scp -r mydir fieldmouse @ madhat:." - will copy the entire "mydir" directory to another system, including all its contents and additional directories. The directory on madhat will still be called mydir. Step 3. What if you have a big mess of files and directories to copy?
You can use the "tar" command to create one file, then copy that file as above, and then use tar to distribute it on another server … But it looks like … not Unix-like. There must be a way to do it in one step, right? Well, of course!
E Enter the pipe of your favorite shell. We can still use tar to package the files we want and then use SSH to get them on a different system (which is what SCP uses under shells), and tar on the other hand to distribute the files back. But why waste time and space creating the tar file itself when we could just create a pipe that spans the two systems and passes tar data through it?
Using the same directory as in the previous example, try "tar -cf - mydir / * | ssh fieldmouse @ madhat 'tar -xf -'"
- Of course, there are other ways to do this as well. Linux is full of tools. Your rating is subject to change.
- You should change the username / hostname / file directory name depending on your network configuration and environment while using the commands above. The commands above are only examples of how to execute commands to copy files to the server.