Copy folder and contents in Linux

I know I can use cp to copy all folder $HOME/data/ to another location. Something like to locally attached USB pen

cp -avr $HOME/data/ /mnt/usbdisk/

What I wanna know is how do we copy folder and contents in Linux to remote server? We have another branch office and they attached external hard disk. So how to tell cp to copy $HOME/data/ to server in another city?

Well, AFAIK you’ve 4 options, the latter 2 not being quite ideal:

  • rsync over SSH.
  • Syncthing. Very involved initially, but when it’s running it’s great. You’ll be having identical copies of folders in multiple places though, but I reckon you’ll be anyway.
  • mount something like SFTP (through SSHFS). Might be unstable, though.
  • use SFTP without mounting.

Both SFTP options are pretty bad in the sense you’re sending data over an FTP protocol (encrypted by SSH), so you need to send whole files (and folders) to update something, which is usually a manual process. So, I don’t recommend [S]FTP.

1 Like

The syntax is as follows:

rsync source user@server:/dest

Copy folder and contents in Linux using rsync to the remote system called serverA

rsync -av $HOME/data/ admin@serverA:/path/to/dir/
rsync -av $HOME/data/ admin@serverA:/mnt/usb/

Where,

  1. -a : Archive mode and copy all files and sub-dirs
  2. -v : Verbose mode, show progess.
  3. $HOME/data/ : local source directory
  4. admin : Remote server username
  5. serverA : Remote server name or IP address
  6. /mnt/usb : Remote server destination directory. Make sure external hard drive mounted at /mnt/usb/ and admin user have access.
1 Like