How to write an SCP command in terminal

SCP is a command from SSH toolset used to copy files from one host to another. Often it is used to copy files from your local PC to the server on which you have SSH access, or reversely from the server to your local PC.

How to write a command:

scp /path/to/file.txt root@8.8.8.8:/path/on/server/to/

Will copy file.txt from your pc to server on IP 8.8.8.8, by accessing ssh for the root user. You should know the password of the root user or have your public key installed to /root/.ssh/authorized_keys on server.

Reverse operation - copy from server to local pc:

scp  root@8.8.8.8:/path/on/server/to/file.txt /path/on/local/machine/

How to write SCP command to copy the folder📂

To copy folder between hosts:

scp -r /path/to/folder root@8.8.8.8:/path/on/server/

WIll copy folder and nested files to /path/on/server/.

⚠ If your folder has a lot of small files inside we strongly recommend not use SCP, but use tar + ssh command to speed the process up:

tar -czf - /path/to/folder | ssh [email protected] tar -xzf - -C /path/on/server/

It is the faster equivalent for the above command. Why: because how long does a server transfer take depends not on size, but on files count. Here we just tar files with simple GZIP compression to one stream and pass this stream over SSH stdio.

#scp #ssh
10
Ivan Borshchov profile picture
Nov 17, 2020
by Ivan Borshchov
Did it help you?
Yes !
No

Best related

Other by Ivan Borshchov