Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Use scp to securely copy files remotely over SSH


    Learn how to copy files to and from remote server hosts with the scp command. You will also learn how to specify a different port and identity file when secure copying files.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: The scp or secure copy command is very similar to the cp or copy command. It allows you to copy files to and from a remote host.

    Start by echoing foo to a file named bar.txt. To use the secure copy command to copy the file to the remote host, type scp followed by the file you wish to copy, in this case bar.txt. Then specify your username @remotehost connection string, and suffix it with a colon.

    After the colon is where to tell scp where to copy the file to on the remote host. In this case, copy it to the home directory. By default, scp will use your default SSH key to connect to the remote host. To specify a different SSH key or identity, use the -i flag followed by the location of your SSH private key.

    If the remote SSH server is bound to a port other than the default port 22, you can specify a -P flag, followed by the port number, in this case 2022. You can also copy entire folders recursively with the -r flag.

    Let's make a foo folder, then create a bar.txt file within the foo folder. We can then recursively copy the entire foo folder with the -r command. To copy a file or a folder from a remote host back to the local host machine, specify the remote connection string first followed by a colon, and then the location of the file or folder on the remote host you wish to copy.

    The second parameter is where on our local host machine you wish to copy the file to, with ./ being the current directory and a new file name if you wish.