Copy Files Using SCP

SCP (Secure Copy Protocol) is a secure file transfer command based on SSH used to encryptively transmit files between a local host and a remote host (or between two remote hosts).

Common Scenarios

  • Transfer files between a local host and a remote host (Local → Remote)
scp /local/file.txt username@remote_IP:/target path/ 

Example:

scp ~/test.zip pi@192.168.2.xxx:/home/pi/ # Copy to the remote host's home directory. 
  • Transfer files between two remote hosts (Remote → Remote)
scp user1@host1:/path/to/file user2@host2:/path/to/dest/ 

Example:

scp alice@server1:/data/report.txt bob@server2:/backups/ 
  • Transfer files from a remote host to a local host (Remote → Local)
scp username@remote_IP:/remote/file.txt /local/path/ 

Example:

scp root@192.168.2.xxx:/var/log/app.log ./downloads/ # Download a remote file to a local host. 

Recursively Copying Directories

Copy an entire folder (including subfiles and subdirectories):

scp -r /local/directory/ user@host:/target path/ 

Example:

scp -r ~/my_project/ root@192.168.2.xxx:/home/pi/