X
wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 14 people, some anonymous, worked to edit and improve it over time.
This article has been viewed 180,043 times.
Learn more...
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.
Things You Should Know
- You can use scp (secure copy) to securely transfer files between two Linux servers.
- To copy a lot of files at once, compress them into a single file using the tar command first.
Steps
-
1For 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.
-
2To 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.Advertisement
-
3What 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 -'"
Advertisement
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.⧼thumbs_response⧽
Advertisement
About This Article
Advertisement