How to Copy Files and Directories Between Linux Servers

We will use the command scp to copy files and directories between servers.

Contents

If you need to copy files or directories within a single Linux server, not between servers, please see this article.

Copy a File

Suppose, you have a file /path/to/file.dump.gz on a source server. And you need to copy it to a destination server.

1. On the destination server, go to the directory where the file needs to be copied:

cd /path/to/destination/directory

2. Run scp:

scp -P 22 source_server_username@source_server_IP:/path/to/file.dump.gz .

, where

  • -P – port number at the source server..
    The default port is 22. But system administrators often change it for security reasons. So I included the port option.

See the scp man page for more options.

You can also add the option -C for compression. But when transferring already compressed files (like jpeg or mp3), this could make the transfer even slower. Usually, I do not add this option.

For example:

  • We have a file /var/www/file.dump.gz on the server 165.227.97.17
  • The server 165.227.97.17 has SSH access on port 22.
  • The SSH user name on 165.227.97.17 is admin
  • And we need to copy this file to some destination server.

The command we should on the destination server should be:

scp -P 22 admin@165.227.97.17:/var/www/file.dump.gz .

After we enter the password for our user admin, this will copy the file /var/www/file.dump.gz from the source server to the current folder on the destination server.

Copy a Directory

To copy a directory /path/to/directory on the source server to the current directory on the destination server:

scp -P 22 -r -p old_server_username@old_server_IP:/path/to/directory .

, where

  • -P – port number at the source server.
  • -r – recursively copy directories. Symbolic links are followed. And copied as files and directories – not as symbloic links.
  • -p – preserve modification time, access time, and file mode.

See the scp man page for more options.

For example:

  • We have a directory /var/www/wpdiaries/images/ on the server 165.227.97.17
  • The server 165.227.97.17 has SSH access on port 22.
  • The SSH user name on 165.227.97.17 is admin
  • And we need to copy this directory to some destination server.

We need to run the command on the destination server:

scp -P 22 -r -p admin@165.227.97.17:/var/www/wpdiaries/images .

After we enter the password for our user admin, this will copy the directory /var/www/wpdiaries/images/ from the source server to the current directory on the destination server.

So we’ll have the subdirectory images created in the current directory on the destination server. With all its subdirectories and files copied from the source server.

Please notice:

  1. Recursive scp will follow symbolic links and copy them as files/directories. Not as symbolic links (source). So please be careful.
  2. If you need to copy a lot of files, this could take a lot of time. In this case, you can use the command screen to detach your session. So that copying would not stop when your terminal window gets closed (or if you are disconnected from the server).

Sergei Korolev
Sergei Korolev
Web developer and the author of all articles on this site. With over 25 years of programming experience, he has specialized in web programming for more than 19 years. He is a Zend Certified Engineer in PHP 5.3. He is available for hire at a rate of $60 USD per hour. You can see his resume here. Please contact him via this contact form to get a quote. He currently lives in Belgrade, Serbia.

Leave a Reply

Your email address will not be published. Required fields are marked *