SCP Command to copy the file from EC2 machine to Local

SCP Command to copy the file from EC2 machine to Local

Last Blog Review

In the last blog we understood, how to copy the files from the local to EC2 machine using scp. scp is actually helpful when you want the files from the local machine to the EC2 machine and vice versa because when you have your code or data which is present at the local machine and that needs to be on the EC2 so scp actually helps a lot instead of writing the same stuff back.

Copy files from EC2 machine to Local machine

  1. Let's say that there is file on EC2 machine named "fil.txt" with content as "Devops" and "devops". Now i want to copy this file from EC2 machine to the local machine(Windows). So, still I will use "scp" command in my local machine(Windows). Lets see how

  2. On my local machine(Windows) first I will go to the directory where I want the file to get copied. So, I want in the downloads directory so i will go to the downloads directory. Make sure that the file you want to get copied on your local machine(Windows) and the private key file (Mihir.pem) of the EC2 machine can be in the same directory as it will be easy to be in same place in cmd instead of changing the directory again and again.

     Local Machine CMD (Windows)
     ---------------------------
     C:\Users\hp>cd Downloads
     C:\Users\hp\Downloads>
    
  3. Then I will use "scp" command as

    "scp -i Mihir.pem ubuntu@ec2-3-94-96-214.compute-1.amazonaws.com:/home/ubuntu/fil.txt ." means "scp" to copy from source to destination next "-i" is identity file, next "Mihir.pem" file for allowing the matching of the private key file(Mihir.pem) & public key file on EC2 machine to allow the secure connection, next "ubuntu@ec2-3-94-96-214.compute-1.amazonaws.com:/home/ubuntu/fil.txt" is the source from where the data has to be copied and "." is the destination where the data will be copied.

     Local Machine CMD (Windows)
     ---------------------------
     C:\Users\hp\Downloads>scp -i Mihir_Grep.pem ubuntu@ec2-3-94-96-214.compute-1.amazonaws.com:/home/ubuntu/fil.txt .
     fil.txt                                                                               100%   14     0.1KB/s   00:00
     C:\Users\hp\Downloads>
    
  4. As you see that the it shows 100% complete of copying with a speed of 0.1kb/s.

  5. Now we will go to our local machine(Windows) and check if the file has been copied to the given location or not properly. See its successfully copied from one server i.e. EC2 machine to other server i.e. local machine(Windows).

Conclusion →

We can send the file from local machine to EC2 and also send the file from EC2 to local machine. Only the requirement is that the scp can be used only on local machine as the local machine must have the private key file and the public DNS of the EC2 machine to allow the secure copy.

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!