How to Copy/Sync Files and Directories with Rsync in Linux : A Beginners Guide to Rsync

Rsync is a very flexible network-enabled syncing tool. It is a powerful tool that facilitates the transfer and synchronization of data between both local and remote systems. Rsync stands for "remote sync". It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed.

With the help of rsync command we can copy and synchronize data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines. It’s faster than scp (Secure Copy) and consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.

Now lets see some example of how we can use rsync in our day to day jobs :

The rsync utility is installed on most Linux/Unix systems by default. But in case, if it is not installed on your system then use the below command to install it :
 $ sudo apt-get update
 $ sudo apt-get install rsync
The basic format of rsync command is as follows :
 $ rsync option(s) [source] [destination]
Some basic options of rsync is as follows :
  • -v verbose
  • -a archive mode
  • -r copy recursively
  • -z compress file data
  • -h human readable output

1. Copy/Sync File/Directory Locally :

To copy or sync file locally in rsync, use the below command.
 $ rsync -zvh backup_files.tar /home/user/bakup
And for archived directory, use the below command.
 $ rsync -azvh docfiles /home/user/bakup
At above example docfiles is a directory, here all the files directory structure inside docfiles will be copied into the /home/user/baskup directory.

2. Copy/Sync File/Directory to a remote server :

To copy or sync files or directories into a remote server, use the below command :
 $ rsync -avz files/ user@192.168.2.120:/home/
The above command will copy the content of files directory into the home directory of remote machine. And also note that on remote machine there is SSHD service is running, by which all the file will be transferred. And when you run the above command, then it will ask you SSH password of remote machine in order to successfully copy/sync the files. If on the remote machine, SSHD service is not running, then the above rsync command will not work.

3. Copy/Sync File/Directory from a remote server :

To copy or sync files or directories from a remote server, use the below command :
 $ rsync -avzh user@192.168.2.120:/home/file/ /tmp/backups/
4. Using rsync over SSH :

by adding 'e' option we can use rsync over ssh, but for this we need root privilege on terminal. For example copy/sync files from remote server to local machine over ssh.
 $ rsync -avhze ssh user@192.168.2.120:/home/docs.txt /tmp/
And from local machine to remote server.
 $ rsync -avhze ssh backup.tar user@192.168.2.120:/home/
5. Delete source file after successful transfer :

By default the rsync will keep the files which are copied, so to delete the source files after successful transfer use the below command :
 $ rsync --remove-source-files -zvh backup.tar /tmp/backup/
6. Show the Progress while transferring data :

To see the progress while transferring the data we just need to add the '--process' option in the command. For example :
 $ rsync --process -zvh backup.tar /tmp/backup/
And at last for manual page, type :
 $ man rsync




Conclusion :

Rsync is a very flexible and powerful tool that facilitates the transfer and synchronization of data between both local and remote systems. At above we see some examples of how we can use rsync. For more information about rsync check the manual page : https://linux.die.net/man/1/rsync