{ scp } > 1 posts

Transferring files via SCP

quick tip

A long time ago in a galaxy far far away, we used to use FTP to upload files. Things like coda, filezilla, Dreamweaver ect. That was when the west was young and I was a baby dev. Today, there are much more robust alternatives. There are tools like Capistrano, Rocketeer, and Deployer for those who want ultimate control and prefer to do it themselves. Then there are other solutions like envoyer.io for fully integrated solutions that remove some of the legwork.

Every once in a while though, You may just need to throw a quick file up on the server ( or many of them ). You may need to download a bunch of stuff or upload a bunch of stuff. In such cases most people just reach for there handy dandy FTP client again. While this solition works, it is very slow. Hence why i'm writing this post. I had to upload a bunch of images the other day to a site with no deployment system in place. I ended up using coda to upload and almost shot myself. Even on my super fast internet the process was a crawl at best. So enter SCP to save the day.

What is SCP?

SCP is a secure copying protocol that is related to SSH. It is much quicker than FTP so let's take a look at how it works.

Downloading a file from remote to local
#Make a local directory to test with
mkdir ~/Desktop/scpTest

#Copy the file from remote to local
scp -P 7822 root@77.38.176.171:/var/www/ghost/README.md ~/Desktop/scpTest  
Uploading a file from local to remote
scp -P 7822 ~/Desktop/scpTest/scp_readme.md root@77.38.176.171:/var/www/ghost  

Notice that with these two the order of things is just reversed. In the first example, downloading a file, you specify the server first. In example two, uploading a file, you specify the file to upload first. Also take note of the -P flag to specify a custom port.

If your host required a custom port and you don't specify the port with the -P flag then the default port will be attempted and the connection will be unsuccessful.

Uploading entire directories from local to remote
#Copies the local directory scpTest to the scpDemo directory on remote.
scp -P 7822 -r ~/Desktop/scpTest/ root@77.38.176.171:/var/www/scpDemo  

The thing to note here is just the inclusion of the -r recursive flag. As you would imagine copying directories from remote to local can be done in the inverse way as we did with files above. Just don't forget the recursive flag.

Conclusion

SCP is faster and more secure than FTP. In addition, it's more convinient once you get the syntax down. So when you cant use a git based deployment solution and you have to transfer some stuff manually, don't hesitate to stay in the terminal.

Until next time. Same Bat time, Same Bat channel.