SSH or Secure Shell is a protocol that allows secure access to remote computers. SSH also comes with scp utility for transferring file between remote computers. It uses the SSH protocol and has the syntax almost similar to the cp command.

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.

Examples for the following file transfer methods are based on the above setup.

Make sure you have SSH access to the remote server with adequate permission to the remote files and folders.

Transfer file using scp

The easiest of these is scp or secure copy. While cp 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 login credential for the command to work when using scp. You can scp files from local to remote and from remote to local.

  1. Copy single file from local to remote using scp.
    $ scp myfile.txt remoteuser@remoteserver:/remote/folder/

    If the target folder (/remote/folder/) is not specified, it will copy the file to the remote user's home directory.

  2. scp from remote to local using a single file .
    $ scp remoteuser@remoteserver:/remote/folder/remotefile.txt  localfile.txt

    Using . as the copy target (replacing localfile.txt will copy the remote file to the current working directory using the same filename (remotefile.txt)

  3. Copy multiple files from local to remote using scp.
    $ scp myfile.txt myfile2.txt remoteuser@remoteserver:/remote/folder/
  4. Copy all files from local to remote using scp.
    $ scp * remoteuser@remoteserver:/remote/folder/
  5. Copy all files and folders recursively from local to remote using scp.
    $ scp -r * remoteuser@remoteserver:/remote/folder/

    remoteuser need to exist and have write permission to /remote/folder/ in the remote system.

    GUI programs such WinSCP can also be used to transfer files between local and remote host using scp methods.

Transfer file using sftp

sftp or Secure FTP, on the other hand works almost exactly like ftp but with a secure connection. Most of the commands are similar and can be used interchangeably. The following sftp example will work exactly as ftp would.

$ sftp user@
Connected to
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% 3740KB 747.9KB/s   00:05    
sftp> bye

WinSCP can also be used for file transfer using SFTP protocol, but with graphical interface. The other popular tool is FileZilla.

Transfer file using rsync

You can also use ssh to secure your rsync session. To do this, use --rsh=ssh or -e “ssh” with your normal rsync commands. The following 2 commands will work exactly the same;

$ rsync -av --delete --rsh=ssh /path/to/source remoteuser@
$ rsync -av --delete -e "ssh" /path/to/source remoteuser@

If these options are not specified, rsync will first try to connect to rsyncd but will automatically fall back to SSH if rsyncd is not running in the remote system.

Mount remote filesystem locally

Remote filesystems could be mounted to the local host and accessed as a local filesystem. Mounting remote filesystem requires SSH access to the remote host and using sshfs.

Discuss the article:

Comment anonymously. Login not required.