# Transfer files and directories to an EC2 instance

Posted on 15 Nov 2015

#### Transferring files

Here we will discuss how to transfer files and directories between your computer and an existing EC2 instance, as well as how to set up aliases for the commands. To upload a file named ‘file.txt’ in the directory ~/Documents/ from your computer into the directory ~/data/ on your EC2 instance, use the following command. All of it goes on one line.

scp -i path/to/key-pair-file/your-key-pair-file.pem ~/Documents/file.txt ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:~/data/

Here, instead of x’s, you will use the numbers of the IP address for you EC2 instance. Notice that there are dashes separating the numbers, and not dots.

Again, the x’s are the numbers in the IP address of your EC2 instance, separated by dashes, not dots.

#### Alias for transferring files

If we don’t want to remember or look up these commands each time we want to transfer files between your EC2 instance, we can make aliases for these commands. First, open the ./bash_aliases file

nano ~/.bash_aliases

Then add the following two functions at the end of the file; you can name them whatever you like.

ec2_upload_func() { scp -i path/to/key-pair-file/your-key-pair-file.pem "$1" ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:"$2" }

ec2_download_func() { scp -i path/to/key-pair-file/your-key-pair-file.pem ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:"$1" "$2" }

Then, after those two functions, create two aliases that call these functions. Add the following two lines to .bash_aliases (again, call the aliases whatever you like)

Next, save and close the .bash_aliases file, and run the command source ~/.bashrc. Now to upload a file from your computer to your EC2 instance, we can use the command

ec2-push path/to/file/file.txt path/of/destination/in/ec2

ec2-pull path/to/ec2/file/file.txt path/to/desination/on/comp/

#### Transferring directories

The commands for transferring whole directories needs is a slight modification of the command to transfer a single file. To transfer a file and its contents from your computer to your EC2 instance, use the command

scp -i path/to/key-pair-file/your-key-pair-file.pem -r path/to/directory/ ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:path/to/destination/directory

To transfer a file and its contents from your EC2 instance, use the command

scp -i path/to/key-pair-file/your-key-pair-file.pem -r path/to/ec2/directory ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:path/to/destination/directory

#### Alias for transferring directories

To create aliases for the above two commands, we need to add two more functions to .bash_aliases. Open .bash_aliases by using the command nano ~/.bash_aliases. Then add the following functions to the end of this file; call them whatever you like:

ec2_upload_dir_func() { scp -i path/to/key-pair-file/your-key-pair-file.pem -r "$1" ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:"$2" }

ec2_download_dir_func() { scp -i path/to/key-pair-file/your-key-pair-file.pem -r  ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:"$1" "$2" }

Then after these two functions, add the two following lines to .bash_aliases