Where to start with Rsync command : 8 Rsync Examples
In this article, we are going to discuss some of the commonly used rsync examples that will familiarise you with rsync & help you get started with using rsync command.
Rsync command is used for transferring of files on Linux machines from one system to another. It uses method on incremental file transfer for copying the files & since the file transfer is incremental, only the difference among the two files is copied & not the complete file as is the case with other tools like scp. Rsync is very lightweight, it is fast as well as secure & also uses very low bandwidth for data copy.
Now before we move onto the rsync examples, let discuss it’s installation.
Recommended Read: SpeedTest-CLI: Run network speedtest on Linux CLI
Though it’s usually is installed on almost all the Linux distributions, one can install it using the following commands,
CentOS/RHEL/Scintific Linux/Oracle Linux/ Amazon Linux
# yum install rsync
# dnf install rsync
# apt-get install rsync
Rsync command can be used with the following syntax,
rsync options source destination
Now let’s discuss some rsync examples,
1- Syncing directories on the Local system
Use the following command to sync a directory to another on the same system,
# rsync -arv /home/user /data/user
Here, option ‘a’ is for ‘archive’, meaning it will preserve all the permissions , links , dates etc
option ‘r’ is for ‘recursive’ meaning it will copy all directories & sub-directories
& option ‘v’ is ‘verbose’, meaning it will show output on the screen of what is being copied.
2- Syncing a local directory to a remote directory
Now this is the mostly used use-case for rsync command & can be used to taking backup of a system to another machine. Use the following example to sync local folder to remote machines,
# rsync -zvh /home/user firstname.lastname@example.org:/data/user
here, option ‘z’ is used for compressing of data during the transfer,
& option ‘h’ is used for printing the output in human-readable format.
3- Syncing a remote directory to a local directory
It is the reverse of the above command. Use the following command,
# rsync -azP email@example.com:/home/user /data/user
here, option ‘P’ is used to show a progress bar for data copy. We can also use ‘–progress’ instead of ‘P’ flag.
4- Secure syncing of data using rsync with ssh
If we need a secure connection with proper encryption to copy the data, we can use the ssh protocol with rsync command. Use the following rsync example as reference,
# rsync -avz -e ssh firstname.lastname@example.org:/home/user /data/user
now after entering the password, the file transfer will be started & will be completely secure.
5- Sync only the directory structure
If we need to copy only the tree structure i.e. how directories & sub-directories are created & does not need to copy the files inside those directories, we can use the option ‘d’,
# rsync -d email@example.com:/home/user /data/user
6- Include or Exclude files during syncing
We can also include some files with a pattern & exclude other files using include & exclude options,
# rsync -avzh –include ‘*.txt’ –exclude ‘*’ /home/user firstname.lastname@example.org:/data/user
here in this example, we are only copying .txt files & excluding all other files.
7- Limiting the file size
With option ‘max-size’, we can also impose a limit on the size of the files that will be synced. Use the following example,
# rsync -arv –max-size=’100K’ /home/user /data/user
8- View the difference of files at source & destination
Rsync also provides option to check differences in the files at source & destination. This can be done using the option ‘i’,
# rsync -avzi /home/user /data/user
Now we should get output like,
Here, the alphabets mentioned before the file name ‘Dan’ & ‘Susan’ tells what the difference in both files are,
f means that it is a file.
s means that there is size change.
t means change in timestamp.
o means change in ownership.
g means change of group.
Now these were only some commonly used rsync examples & there are many other rsync options that can be used but these will get you started. Please do let us know about any query or question you have regarding this tutorial using the comment box below.
If you have found this article to be useful, please share it among your friends/colleagues/followers. THANKS !!!
If you think we have helped you or just want to support us, please consider these :-
Donate us some of your hard earned money:
Linux TechLab is thankful for your continued support.
Originally published at https://linuxtechlab.com/start-with-rsync-command-8-rsync-examples/ on July 13, 2018.