I am Hack Sparrow
Captain of the Internets.

Linux scp command – Common use cases

How to use the Linux scp command

scp is a very useful Linux tool which lets you copy files from one system to another via SSH in a secured manner. I will assume that, since you want to use the scp command, you have access to a Linux or related system. You can find out details about the scp command by doing man scp at the terminal. But that's not the point of it. I want to show you some common uses cases of scp and how to do it.

How to copy a file

$ scp kitteh.jpg sparrow@123.45.678.912:/home/sparrow/lolcats/

In this case kitteh.jpg will be uploaded to the directory called lolcats on 123.45.678.912 for the user sparrow. You will be prompted for sparrow's password.

How to copy multiple files

$ scp kitteh0.jpg kitteh1.jpg sparrow@123.45.678.912:/home/sparrow/lolcats/

In this case scp will copy kitteh0.jpg and kitteh1.jpg to the lolcats directory.

How to copy a directory

$ scp -r kittehs sparrow@123.45.678.912:/home/sparrow/lolcats/

In this case the kittehs directory along with its contents will be copied to the lolcats directory - a directory called kittehs will be created under lolcats.

How to upload a file with a new name

scp checks if the destination is a directory, if yes, it uses the existing filename for the file name copied to the destination. If you want to give a new name to the file after uploading, mention a file name in the destination.

$ scp kitteh.jpg sparrow@123.45.678.912:/home/sparrow/lolcats/kitten.jpg

In this case kitteh.jpg will be uploaded to the lolcats directory as kitten.jpg.

How to upload files using identity files / .pem files?

scp has an -i option using which you can specify an identity file (commonly a .pem file) to scp's SSH connection. The advantage of using an identity file is that you don't have to keep typing in the destination user's password for each operation.

Take a look at the -i version of two of the previous commands:

$ scp -i captain.pem kitteh.jpg sparrow@123.45.678.912:/home/sparrow/lolcats/
$ scp -i captain.pem -r kittehs sparrow@123.45.678.912:/home/sparrow/lolcats/

Just add -i and the identity file name right after scp, and keep everything the same. It is a good idea to keep a copy of the identity file in the main directory where you intend to do the scping.

So that's all about some of the very common use cases of the Linux command scp. If you wanna know more, type man scp at the terminal.

Make a Comment