If you need to copy files from your local machine to a remote server using SSH protocol, you can do it by using the SCP
command on Linux Ubuntu, Linux Mint and Debian as others.
The SCP
utility comes by default with SSH, it is used for transferring files between remote computers. It uses the SSH protocol and has the syntax almost similar to the cp
(copy) command.
SSH, or Secure Shell, is a protocol that allows secure access to remote computers.
Other file transfer applications such as sftp
and rsync
can also utilize SSH to secure their file transfers. These applications allow us to copy our files from local to remote servers and to copy files from remote servers to our local machine.
What is SCP command on Linux
scp
, or Secure File Copy, is a program for copying files between remote computers. It uses the SSH protocol and is included by default in most Linux and Unix distributions, like Linux Ubuntu, Linux Mint, Linux Debian, Arch Linux, etc. It is also included in the OpenSSH packages.
With SCP
you can easily and securely transfer files between a remote location and a host, between two remote locations, or copy from your local computer to a remote server.
Linux SCP via SSH: how to copy files
While cp
(copy command on Shell) is for copying local files, scp
is for remote file transfer. The main difference between cp
and scp
is that, you’ll have to specify the remote host’s DNS name or IP address and provide a username credential for the command to work. You can copy files with scp
from your local to remote and from remote to local.
Linux SCP local to remote: copy single file
The syntax to copy a single file from you local computer, that can be you current folder ou any other folder on your local computer, to a remote computer is:
# From current folder you don't need to specify the file path
scp myfile.txt [email protected]:/remote/folder/
# From any folder from your local computer, write the full local path to the file
scp /full/path/to/myfile.txt [email protected]:/remote/folder/
If the target folder on remote host (/remote/folder/
) is not specified, it will copy the file to the remote user’s home directory.
The user specified with username
need to exist and have write permission to /remote/folder/ in the remote system.
Linux SCP remote to local: copy single file
You can also to copy a file that exist in a remote server computer, to your local computer using scp
. The syntax is similar, basically you just need to invert the order of the local and remote informations:
# From remote to current local folder
scp use[email protected]:/remote/folder/remotefile.txt localfile.txt
# From remote to local but specifying the local folder target to save the file
scp [email protected]:/remote/folder/remotefile.txt /path/to/local/folder/localfile.txt
Using .
(a dot) as the copy target (in our example replacing localfile.txt
to .
, will copy the remote file to the current working directory using the original filename, in this case: remotefile.txt
Copy several files using SCP
To copy multiple files from your local machine to a remote server, or vice-versa, you can pass each original file as a parameter, or use wildcards:
# Specify each file
scp myfile.txt /local/folder/myfile2.txt [email protected]:/remote/folder/
# Use a wildcard to copy several files from remote
scp [email protected]:/remote/folder/* .
Copy files and folders recursively from local to remote using SCP
To copy recursively files and folders from local to a remote server, you need to use the option -r
.
This can be useful to build the same structure you have local to a project on a remote server:
scp -r * [email protected]:/remote/folder/
Copy files using SCP with PEM or CER credential
If you don’t want to type the remote user password when are copying files, you can use a .pem
ou .cer
file to inform you secure credential to scp
ssh connection.
Note that you still need to write the username on the command:
scp -i ~/.ssh/my-certificate.cer my-local-file.html [email protected]:/www/my-project/
Conclusions
One of the most useful and quick ways to transfer files in a secure mode between remote located computers connected by a network (internet) is by using scp
Linux command.
Comments