How to copy files from one Linux machine to another using ssh

To copy files from one machine to another machine using ssh, you can do that in 3 ways,

  • SCP
    • scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same authentication and provides the same security as ssh(1). scp will ask for passwords or passphrases if they are needed for authentication.
    •  $ mkdir local_folder
       $ cd local_folder
       $ echo "hello copy world" > this_is_test_file.txt
       $ scp this_is_test_file.txt remote_username@remote_user_ip:/home/remote_username/Desktop

      This above command will copy a single file (this_is_test_file.txt) from the local machine to the remote machine’s desktop.
      If you wan to copy entire folder to remote machine, we need to use scp command with “-r” as,

       $ scp -r local_folder remote_username@remote_user_ip:/home/remote_username/Desktop 
    • Same way, if we want to copy remote machine's files/folders to local machine, just need interchange source and destinations as,
       $ scp -r remote_username@remote_user_ip:/home/remote_username/Desktop/remote_folder local_folder 
  • Rsync
    • Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.
      •  $  rsync -av -e "ssh" local_folder remote_username@remote_user_ip:/home/remote_username/Desktop

        As mentioned before, benefit of rsync over scp is that if rsync has copied some files from the folder and network/connection got broken, and then you restarted same command then only files which has not been copied in previous rsync attempt will get copied, saving both time and bandwidth such is not the case with where everything will get copied again ( with posibility of breaking again if connection is unstable )

  • Remote Mount using sshfs
    • SSHFS (Secure SHell FileSystem) is a file system for Linux (and other operating systems with a FUSE
      implementation, such as Mac OS X or FreeBSD) capable of operating on files on a remote computer using
      just a secure shell login on the remote computer