PDF download Download Article
Use scp to securely move files between servers
PDF download Download Article

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. This wikiHow article will teach you how to use the scp command to securely transfer files and directories from one Linux system to another.

Things You Should Know

  • The syntax for the scp command is scp filename username@hostname:filename .
  • To transfer a directory, use scp -r /path/to/local_directory username@hostname:/path/to/remote_directory .
  • To copy multiple individual files at once, add them to a tarball first.
  1. You can use this as a "push" or a "pull" command, but let's start with pushing the file to the other server. 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.
  2. 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 /path/to/local_directory username@hostname:/path/to/remote_directory will copy the entire directory "/path/to/local_directory" over to the other system in its /path/to/remote_directory directory, including all of its subdirectories.
    Advertisement
  3. 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, 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 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/*
  4. Advertisement


Expert Q&A

Ask a Question
      Advertisement

      Tips

      • Using the above commands, You should replace the user name/hostname/file name/directory name according to your network configuration and environment. The commands shown above are only examples of executing commands for copying files on the server.
      • 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.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 195,523 times.

      Is this article up to date?

      Advertisement