Blog

how-to-transfer-files-using-scp-with-examples-identicalcloud

how to transfer files using scp – all commands with example

While using Linux servers, there are many times where you need to transfer files from local to server or server to local or one server to another server. SCP is Secure Copy command, which copies files and folders from the server to the local machine and vice versa, using SSH protocol. There can be one file or folders of more than 1 file. With SCP commands, you can download or upload files to your server using the command line. Here are list of commands which will help you transfer those files using the command line

1. Transferring file from local to server

To Transfer a file from your local machine to server using pem file

scp -i /path-to-pem-file/file.pem /path-to-file/filename.txt ubuntu@1.2.3.4:/location-of-server/filename.txt

If password authentication is enabled, this command will prompt for password

scp /path-to-file/filename.txt root@1.2.3.4:/location-of-server/filename.txt

PS: While running any of these commands, if you are doing SSH or SCP the first time, it will ask you to accept the fingerprint received, so you can say, you trust this host. If you want to avoid being popped about that, use -o StrictHostKeyChecking=no

PS: While running any of these commands, if you are doing SSH or SCP the first time, it will ask you to accept the fingerprint received, so you can say, you trust this host. If you want to avoid being popped about that, use -o StrictHostKeyChecking=no

scp -o StrictHostKeyChecking=No -i /path-to-pem-file/file.pem /path-to-file/filename.txt ubuntu@1.2.3.4:/location-of-server/filename.txt

2. Transferring file from server to Local

To Transfer a file from your server to local machine using pem file

scp -i /path-to-pem-file/file.pem ubuntu@1.2.3.4:/location-of-server/filename.txt /path-to-file/filename.txt

If password authentication is enabled, this command will prompt for password

scp root@1.2.3.4:/location-of-server/filename.txt /path-to-file/filename.txt

3. Transferring file from one server to another server

To transfer file from one server to another, you can SSH into any machine and use any of the above commands to transfer/upload/download files. But what if, you don’t have access to any server, then use below-mentioned command from you local machine

scp root@1.2.3.4:/location-of-server-1/filename.txt root@5.6.7.8:/path-to-file/filename.txt

4. Transferring directory/folder from server to Local

To Transfer a file from your server to local machine using pem file

scp -i /path-to-pem-file/file.pem -r /path-to-folder/ ubuntu@1.2.3.4:/location-of-server/folder 

If password authentication is enabled, this command will prompt for password

scp -r /path-to-folder/ root@1.2.3.4:/location-of-server/folder 

This command will transfer all files and folders that resides in the folder

5. Transferring directory/folder from local to Server

To Transfer a file from your local machine to server using pem file

scp -i /path-to-pem-file/file.pem -r ubuntu@1.2.3.4:/location-of-server/folder /path-to-folder/  

If password authentication is enabled, this command will prompt for password

scp -r root@1.2.3.4:/location-of-server/folder /path-to-folder/ 

6. Transferring directory/folder from one server to another Server

In this case, you can SSH into one server and then use any of above 2 commands to copy files, but if you don’t have direct SSH access, then use following

scp -r root@1.2.3.4:/path-to-folder/ root@5.6.7.8:/location-of-server/folder 

Notes:

There are some other tags which can be helpful while using scp commands

  • -P : If your SSH port is different than 22, use -P tag to mention port number
  • -C: If you have large files, you can compress them in transit, using -C command
    1. Here, compression of files will only be applied in transit, so at destination, file will be pasted as it is
  • -i : As you already know, to mention server’s key file path, use -i tag
  • -r : To copy files recursively (all files in a folder), use -r tag
  • -p : To preserve file permission and access times, use -p tag

Drafted On,
22nd January 2022
DevOps @identicalCloud.com

Leave a Comment