Linux

How to Copy Files Between Two Machines with SSH

February 28, 2023

This guide is part of the "Snippets" series. This series is focused on providing simple and accessible tutorials on various topics relating to development!


To copy files from one machine to another using SSH, you can use the scp (secure copy) command. First, open a terminal window on the machine from which you want to copy the files.

Then, enter the following command:

scp /path/to/local/file username@remote:/path/to/remote/directory

In this command, you should replace /path/to/local/file with the path to the file on your local machine that you want to copy, and replace username@remote with the username and hostname or IP address of the remote machine. Finally, replace /path/to/remote/directory with the path to the directory on the remote machine where you want to copy the file.

For example, if you want to copy a file called example.txt located in the /home/user/Documents/ directory on your local machine to a directory called data in the home directory of a user called user2 on a remote machine with IP address 192.168.1.100, you would use the following command:

scp /home/user/Documents/example.txt user2@192.168.1.100:~/data/

After you press Enter, you will be prompted to enter the password for the remote machine. Once you have entered the password, the file will be copied to the remote machine.

If you want to copy a directory and its contents, you can use the -r option with the scp command. For example:

scp -r /path/to/local/directory username@remote:/path/to/remote/directory

This will copy the entire directory and its contents to the remote machine.

SCP stands for "Secure Copy". It is a command-line tool for securely transferring files between a local and a remote host over an SSH protocol. SCP provides encryption and authentication for file transfers, ensuring that files are transferred securely.

The SCP tool is included in most Unix-like operating systems, including Linux and macOS. It is also available for Windows as part of the PuTTY suite of tools.

Here are some helpful links to learn more about SCP:

SCP documentation: https://man.openbsd.org/scp.1


You might also like

The latest from the blog