46

I connected to Amazon's linux instance from ssh using private key. I am trying to copy entire folder from that instance to my local linux machine .

Can anyone tell me the correct scp command to do this?

Or do I need something more than scp? Both machines are Ubuntu 10.04 LTS

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Mansoor Elahi
  • 941
  • 1
  • 9
  • 19
  • Probably better suited for http://askubuntu.com, but depending on what you need to do, there are multiple ways to do it. You can use bare `scp` like `scp -r user@remotehost:/path/to/directory /path/to/local` to copy a directory from a remote machine to your local. If you need to keep directories in sync, you can use `rsync`. – wkl Feb 17 '12 at 14:53

10 Answers10

87

another way to do it is

scp -i "insert key file here" -r "insert ec2 instance here" "your local directory"

One mistake I made was scp -ir. The key has to be after the -i, and the -r after that.

so

scp -i amazon.pem -r ec2-user@ec2-##-##-##:/source/dir /destination/dir
Zymawy
  • 1,511
  • 16
  • 15
Raymond Lui
  • 981
  • 6
  • 2
57

Call scp from client machine with recursive option:

scp -r user@remote:src_directory dst_directory
barti_ddu
  • 10,179
  • 1
  • 45
  • 53
  • 10
    this solution helped actually i was trying to copy data without using private key.. scp -i *.pem -r user@remote:src_directory dst_directory – Mansoor Elahi Feb 20 '12 at 06:10
16
scp -i {key path} -r ec2-user@54.159.147.19:{remote path} {local path}
  • 2
    Please try to avoid just dumping code as an answer and try to explain what it does and why. Your code might not be obvious for people who do not have the relevant coding experience. – Frits Sep 22 '16 at 07:29
  • yes, While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – J. Chomel Sep 23 '16 at 08:43
7

For EC2 ubuntu

go to your .pem file directory

scp -i "yourkey.pem" -r ec2user@DNS_name:/home/ubuntu/foldername ~/Desktop/localfolder
3

You could even use rsync.

rsync -aPSHiv remote:directory .
glglgl
  • 89,107
  • 13
  • 149
  • 217
2

This's how I copied file from amazon ec2 service to local window pc:

pscp -i "your-key-pair.pem" username@ec2-ip-compute.amazonaws.com:/home/username/file.txt C:\Documents\

For Linux to copy a directory:

scp -i "your-key-pair.pem" -r username@ec2-ip-compute.amazonaws.com:/home/username/dirtocopy /var/www/

To connect to amazon it requires key pair authentication.

Note:

Username most probably is ubuntu.

R T
  • 4,369
  • 3
  • 38
  • 49
1

This is also important and related to the above answer. Copying all files in a local directory to EC2. This is a Unix answer.

Copy the entire local folder to a folder in EC2:
scp -i "key-pair.pem" -r /home/Projects/myfiles ubuntu@ec2.amazonaws.com:/home/dir

Copy only the entire contents of local folder to folder in EC2:
scp -i "key-pair.pem" -r /home/Projects/myfiles/* ubuntu@ec2.amazonaws.com:/home/dir

Edwinner
  • 2,458
  • 1
  • 22
  • 33
1

I use sshfs and mount remote directory to local machine and do whatever you want. Here is a small guide, commands may change on your system

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
qwerty
  • 2,065
  • 2
  • 28
  • 39
0

One way I found on youtube is to connect a local folder with a shared folder in EC2 instance. Please view this video for the full instruction. The sharing is instantaneous.

Rabindra
  • 369
  • 4
  • 14
0

I do not like to use scp for large number of files as it does a 'transaction' for each file. The following is much better:

cd local_dir; ssh user@server 'cd remote_dir_parent; tar -c remote_dir' | tar -x

You can add a z flag to tar to compress on server and uncompress on client.

jfg956
  • 16,077
  • 4
  • 26
  • 34