How to transfer files using scp command

For each of the following methods, make sure you have the right credentials to each remote server you are working. You need the right pair of username and password of each machine for a successful transfer.

Transfer files from server1 to server2

Scenario: You have a folder named migrate_this located at the home folder of server1_username and you need to copy it and all its content to the home folder of server2_username.

Login to server1 and issue this command
scp -r migrate_this server2_username@server2_ip_address:~/

The code above will copy migrate_this to the home folder of server2_username. If you wish to copy migrate_this to a different location, replace the ~/ with the proper directory. Example is...
sudo scp -r migrate_this server2_username@server2_ip_address:/var/www/my_site/

With scp, you don't actually need to login to server1 to do the transfer.
scp -r server1_username@server1_ip_address:~/migrate_this server2_username@server2_ip_address:~/

Transfer files from your local machine to a remote server

Scenario: You have in your local machine a folder named migrate_this and you need to upload it, and all files inside it, to a remote machine.

From your Terminal, cd to the location containing the migrate_this folder.
scp -r migrate_this username@sremote_server_ip_address:~/

Category