Files exchanging between AWS EC2 and your local machine

Image for post
Image for post

Copying files from local to EC2

  • To copy files between your computer and your instance you can use an FTP service like FileZilla or the command scp. “scp” means “secure copy”, which can copy files between computers on a network. You can use this tool in a Terminal on a Unix/Linux/Mac system.
  • To use scp with a key pair use the following command:

scp -i /directory/to/abc.pem /your/local/file/to/copy user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:path/to/file

Image for post
Image for post
Ref: Screenshot from terminal
  • You need to specify the correct Linux user. From Amazon:
    For Amazon Linux, the user name is ec2-user.
    For RHEL, the user name is ec2-user or root.
    For Ubuntu, the user name is ubuntu or root.
    For Centos, the user name is centos.
    For Fedora, the user name is ec2-user.
    For SUSE, the user name is ec2-user or root.
    Otherwise, if ec2-user and root don’t work, check with your AMI provider.
  • To use it without a key pair, just omit the flag -i and type in the password of the user when prompted.

Note: You need to make sure that the user “user” has the permission to write in the target directory. In this example, if ~/path/to/file was created by user “user”, it should be fine.

Copying files from EC2 to local

scp -i /directory/to/abc.pem user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:path/to/file /your/local/directory/files/to/download

Image for post
Image for post
Reference: Screenshot from terminal

#Hack 1: While downloading file from EC2, download folder by archiving it.
zip -r squash.zip /your/ec2/directory/

#Hack 2 : You can download all archived files from ec2 to just by below command.

scp -i /directory/to/abc.pem user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:~/* /your/local/directory/files/to/download

Happy exploration.

Written by

CTO || Cloud and Distributed Systems Specialist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store