How to copy data over SSH with secure copy (scp)

Linux scp command

scp  or  secure copy is a command used for sending files over SSH. You can copy files between computers, say, from your local PC to a server or vice versa. It’s a very secure method of copying files between computers. scp uses port 22.

The command is quite similar to cp  used in copying local files, expect that you have to specify the remote user and host in your command. The basic syntax is scp mytestfile.txt remoteusername@hostname:/remote/folder/path . You can also copy files from one remote server to another remote server, without passing traffic through your PC.

For example, to move a file from Computer A (local) to Computer B (192.168.170.29) run the command scp testfile.txt [email protected]:/

The above command specified the root destination folder. The admin login doesn’t have permission to write files here.

Linux scp file command root directory
Linux scp file command root directory

 

If we specify a different remote file path scp testfile.txt [email protected]:/srv/ the file is copied correctly.

Linux scp basic command
Linux scp basic command

 

A few examples:

Copy a file to a remote destination
scp /path/to/source/file user@hostname:/path/to/destination/

Copy a file from a remote server to a local folder
scp user@hostname:/path/to/source/file /path/to/destination/

Copy a single file between remote servers
scp userA@hostA:/path/to/source/file userB@hostB:/path/to/destination/

Copy remote file to current location
scp admin@hostname:testfile.txt .

Copy a file to the users home folder
scp testfile.txt user@hostname:

Just like the cp command you can copy multiple files, folders, or files matched by wildcards. For example:

Copy multiple files
scp fileone.txt filetwo.txt user@hostname:/path/to/destination/

Copy files with an extension .txt
scp /path/to/source/*.txt user@hostname:/path/to/destination/

Copy all files in a folder
scp /path/to/source/* user@hostname:/path/to/destination/

Copy all files in folder (recursively)
scp -r /path/to/source/ user@hostname:/path/to/destination/

You May Also Like