SCP – Moving large files from one Linux server to your new Linux server
What is scp in linux?
Ok, so you like to move your 3 gig backup from your old server to your new server, but the good old wget command is not doing the trick?
There is an easy way around with the scp command
Log into your old server where you want to move that big tar file from and
scp yourlarge3gigfile.tgz root@newserverIP.com :/home/
(enter the root pass for the new server and let it copy the file over)
If you run SSH on a different port (that is, not the default port 22) and you need to scp then it can be done by:
scp -P <port> yourlarge3gigfile.tgz root@newserverIP.com :/home/
For example, if your ssh/scp is on port 2000 then:
scp -P 2000 yourlarge3gigfile.tgz root@newserverIP.com :/home/
the above command will connect to scp/ssh port 2000 and copy the file to the remote user’s home directory in the remote server.
If you use SSH key based authentication then when you scp, scp will not ask for user’s password. It will copy your file to the remote server using your ssh private key. Example,
scp -i your_private_key yourlarge3gigfile.tgz root@newserverIP.com :/home/
the above command will use your ssh private key and copy the file (file.txt) in the remote_user’s home directory.
Running scp Recursively
While it is sometimes faster for large transfers to be done in a single archive known as a tarball, most of the time the overhead of transferring individual files doesn’t matter.
Copying recursively with scp
is easy: simply use the -r
flag in addition to anything else you had added:
scp -r localpath user@remote:/remotepath
Note that this is explicitely lowercase -r
, unlike a lot of other commands that use or require -R
.
This will act like a drag and drop into /remotepath/
, copying the contents of localpath/
to the remote and placing them in /remotepath/localpath/
.
Using rsync Instead
Linux has multiple tools to handle this job, and one of the better ones is rsync
, which does everything scp
can do, but has many more options and is much faster to boot. It also doesn’t copy files that haven’t been changed, making it a great tool for continously “syncing” two directories to each other without re-transferring data unnecessarily.
rsync
works basically the same as scp
, with a few more options included to specify the settings:
rsync -a -essh localpath/ user@remote:/remotepath/
The -a
flag specifies archive mode, which turns on a lot of commonly used options all at once. The -e ssh
flag sets up rsync
to transfer over SSH.