Very often I face the need to copy files to or from the server via SHH, and SCP always helps me with this.

SCP (Secure Copy) is a protocol designed to remotely copy a file between hosts on a network. It uses SHH to transfer data, along with similar authentication and security measures.

The essence of the protocol is that when the destination file already exists, SCP overwrites it with the original file. If there is no destination file yet, an empty file will be created that will receive the name and contents of the copied file.

In this article, I want to put together a small cheat sheet with 10 examples of copying a file using SCP.

Example 1: Copying filename.txt from a remote server to your local computer.

scp username@remote_server_ip:filename.txt /your/local/directory

Example 2: Copying filename.txt file from local host to remote server.

scp filename.txt username@remote_server_ip:/your/remote/directory

Example 3: Copying the dir1 folder from the local computer to the dir2 directory on the remote server.

scp -r dir1 user@remote.host:/your/remote/directory/dir2

Example 4: Copying filename.txt from one remote server remote_server_ip1 to another remote server remote_server_ip2.

scp user@remote_server_ip1:/directory/filename.txt user@remote_server_ip2:/your/directory/

Example 5: Copying the files filename1.txt and filename2.txt from your local computer to your home directory on the remote server.

scp filename1.txt filename2.txt user@remote_server_ip:~

Example 6: Copying filename.txt from local host to remote computer using port 2222.

scp -P 2222 filename.txt user@remote_server_ip:/your/remote/directory

Example 7: Copying the file filename.txt from the local host to your home directory on the remote server. With the preservation of the modification time and access time and rights of the copied file.

scp -p filename.txt user@remote_server_ip:~

Example 8: Copy the file filename.txt from your local computer to your home directory on the remote host. With the increasing speed of SCP, changing the encryption algorithm from AES-128 (default) to Blowfish.

scp -c blowfish filename.txt user@remote_server_ip:~

Example 9: Copying filename.txt from your local computer to your home directory on a remote server. With the limitation of the channel width used by the SCP command to 100 Kbit/s.

scp -l 100 filename.txt user@remote_server_ip:~

Example 10: Copying multiple files from a remote host to the current directory on your local computer.

scp user@remote_server_ip:~/\{file1,file2,file3\} .

Hopefully copying files over SSH makes it a little easier for you using this cheat sheet and the 10 examples above.

Share post
Twitter Facebook