How to copy the files to the remote server using SSH

There are 3 Methords to transfer files remotely

Transfer file using scp
Transfer file using sftp
Transfer file using rsync

Method 1. SCP file transfer Method.

Basic syntax for SCP

scp <OPTIONS> <SOURCE_FILE_OR_DESTINATION> <TARGET_FILE_OR_DESTINATION> <USER>@<HOST_OR_IP>:<REMOTE_DESTINATION>

In case if you want to copy complete directory use -r command for <OPTIONS>

scp <OPTIONS> -r <SOURCE_FILE_OR_DESTINATION> <TARGET_FILE_OR_DESTINATION> <USER>@<HOST_OR_IP>:<REMOTE_DESTINATION>

Example :

Copy single file from local to remote.

scp file.txt user@host_or_ip:/remote/folder/

Copy multiple files from local to remote.

scp file.txt file.txt user@host_or_ip:/remote/folder/

Copy all files from local to remote.

scp * user@host_or_ip:/remote/folder/

Copy all files and folders recursively from local to remote.

scp -r * user@host_or_ip:/remote/folder/

Copy single file from remote to local.

scp user@host_or_ip:/remote/folder/file.txt  file.txt

If in case of any permission issues you can try

scp -oIdentityFile=keyfile  <LOCAL_FILE> user@host_or_ip:/remote/folder/

Method 2: Transfer file using SFTP or Secure FTP

sftp user@YOUR_HOST
Connected to YOUR_HOST.
sftp> dir file1 file2 file3
sftp> pwd
Remote working directory: /home/user
sftp> get file2
Fetching /home/user/file2 to file2
/home/user/file2     100% 500KB 900.9KB/s 00:05
sftp> bye

Method 3: Transfer file using rsync

The rsync stands for remote sync and is a tool for remote and local file synchronization.You can also use SSH to secure the rsync session.The given below are the two commands for rsync.

rsync -av --delete --rsh=ssh /path/to/source user@your_host_ip:/remote/folder/
rsync -av --delete -e "ssh" /path/to/source user@your_host_ip:/remote/folder/

Leave a Reply

Your email address will not be published.Required fields are marked *