Easy Way To Copy Files Over SSH

SSH is a protocol that can help you manage a remote computer, as well as let you upload and download files. You can also use SSH keys instead of a password!
Copying files requires the secure copy command, which is actually pretty simple:

scp [options] [original file] [destination_file]

The syntax for the [destination file]  portion is usually where users have the most difficulty. When you are addressing a remote file you should use this format:


The server, in this case, can either be an IP address or a URL. After the IP/URL address you'll just write the standard file path after a colon.. Here’s an example:

scp –P 40050 Desktop/filename.txt nms@

We used the -P flag here which let’s you specify the port you want to use. Otherwise, it'll just default to port 22. Note that this flag is case sensitive and should be uppercase.  Depending on how your system is configured you might not need to use this flag.
Then I just used the original file, which is “filename.txt,” and it is located in the “Desktop” directory. The destination file is located in “~/Desktop/filename.txt”. The command has been executed by the user “nms” on computer “”.
What if you need to do the reverse operation and copy a file from a remote server to your local machine? You would then switch the source and destination within the same command.

Here’s an example:

scp –P 40050 nms@ Desktop/filename.txt

I will have copied a file from the folder” ~/Desktop/” of the remote computer to the “Desktop” folder on my computer.
If you want to copy a whole directory you will have to use the -r flag, in lowercase. The command would then look like this:

scp -r –P 40050 nms@ Desktop/filename.txt

You can actually “combine” flags. Instead of using:

scp –P –r …

You can use:

scp –Pr …

SSH Without Passwords

Secure copy is great as you can put it in scripts and let it perform backups to remote computers. However, you won’t always be around to provide the password. Being asked to enter a password every time you run the script can get tiresome.Thankfully, you can use key files to get around this problem. You can generate two key files; a private one for your computer and a public one for the remote computer. You’ll then be able to use these instead of a password

First use the following command on your computer:

ssh-keygen –t rsa

The command will generate two keys which should be kept in:


with the name “id_rsa.pub” for the public key and the name “id_rsa” for the private key.
You’ll be asked about where to save the keys. If you want to use the defaults, just hit Enter and you will also be asked to type a passphrase. You can hit the Enter key again to leave it blank.
You should then copy the file with the public key to the remote computer. Now the “scp” command can be used to put the keys in place, like this:

scp -r –P 40050 .ssh/id_rsa.pub nms@

The destination of the key on the remote server will be located in:


And you’re done!
You’ve learned how to use the scp command by itself, as well as generating key files, and using scp with key key files to copy data without using a password. Secure copy over SSH is very flexible. You can use the command for small operations or in conjunction with server control panels for security and flexibility in bulk operations.

February 24 2016

Add or review comments

Please leave your comment

Existing comments

Comments 9

David Pickett
I always liked something more like: $ find ... -type f | cpio ... | gzip -9 | ssh user@system 'gunzip | cpio ...' It allows you to select your own favorite compression and level, distributes the load over many cores/CPUs, relieves ssh of secondary tasks so it just does d/encryption. If directory creation is not an issue, it can be parallelized by tools like GNU parallel. Even then, you can do directory creation first, as it is not CPU or net bandwidth sensitive/intensive. Fun for all those cores! Options for cpio can be set for less compatible hosts, update only if older.
David Pickett
PS: When bandwidth is low, slower but more intense compressors like bzip2 may be worthwhile to minimize that hit. The slower speed can be compensated by more parallelism. (Leave some bw for others!) At very least, consider scp with high compression, possibly in n parallel sessions, to speed things and spare some bw for others. Each scp could be give m file names, for instance. This spares the cost of SSH session setup.
I presonally like the rsync option over ssh more... since it can continue where it left off when the pipe breaks or the network stalls. Especially useful for large collections of files and directories preserving rights... Works almost the same as scp but can also handle updates. <code> rsync -rvah --progress [localDir[ [user]@[remote host]:[remote path] </code>
When the number of files to transfer is small, scp directly is a good option. However, when there are many files or big size of them, or the connection can be lost in progress (wireless) rsync has advantages as it can restart easily.
levitra 200mg [url=http://levitranrx.com]levitra 20 mg [/url] about levitra for's and against <a href="http://levitranrx.com">levitra online </a> - levitra pills forum rules buy levitra uk
levitra 5mg canada knowledge base [url=http://levitrastr.com]levitra 20 mg [/url] levitra dose mg display posts from previous <a href="http://levitrastr.com">generic levitra </a> - levitra 5 mg online posts per day levitra powered by bellabook keyword
Привет всем участникам! прикольный у вас сайт! Нашел прикольную базу кино: [b] смотреть хорошую космическую фантастику [/b] [url=http://kinovalenok.tv/]http://kinovalenok.tv/[/url] Тут: лучшие фильмы отечественные бесплатно http://kinovalenok.tv/otechestvennyy/ список 2019 Здесь: приключения шурика смотреть бесплатно в хорошем качестве http://kinovalenok.tv/priklyucheniya/ список 2019 Тут: фильмы 2019 смотреть хорошем hd новинки http://kinovalenok.tv/novinki/ рейтинг 2019 Здесь: http://kinovalenok.tv/263-beloe-solnce-pustyni-1970.html [b] Смотреть Белое солнце пустыни (1970) онлайн бесплатно [/b] Здесь: [url=http://kinovalenok.tv/9294-prizrakam-bagrovogo-pika-nechego-delat-v-kitae.html] Призракам «Багрового пика» нечего делать в Китае [/url] [b] Призракам «Багрового пика» нечего делать в Китае [/b]
cheap viagra bangkok [url=http://withoutdoctorx.com]viagra without prescription [/url] 20mg viagra dosage <a href="http://withoutdoctorx.com">viagra without doctor </a> - viagra vs viagra prices gb.php?id= http:juki.host-page.com4083buy+viagra+-+generic+and+brand.htmldiscount-viagra
cialis tadalafil 20mg reviews clever.cgi?page= [url=http://walmart-cialis.com]cialis over the counter [/url] cialis generic date ascending order <a href="http://walmart-cialis.com">cialis over counter walmart </a> - genaric cialis viagra cialis combined
<a href="http://canadiantousapharmacy.com/#">canadiantousapharmacy.com</a> canadian rx drugstore online india [url=http://canadiantousapharmacy.com/#]http://canadiantousapharmacy.com/[/url] canadian pharmaceuticals reviews http://canadiantousapharmacy.com/#tadalafil-20-mg <a href="http://avantusproperties.net/__media__/js/netsoltrademark.php?d=http://canadiantousapharmacy.com/#">aarp recommended canadian online pharmacies</a> <a href="http://xn--90aacm0bi2b.xn--p1ai/bitrix/rk.php?goto=http://canadiantousapharmacy.com/#">canadian pharmacy</a>

Get notified about new publications and product updates.
Please note we do not share information to anyone.