12 Linux Rsync Examples,you simply like it,excellent must read it

By | February 11, 2017

By word Rsync means remote sync.Rsync command is used to sync two folders locally or remotely across hosts.Rsync has upper hand compared to “scp” command because it uses encryption and decryption while sending and receiving.rsync not only copies data ,it also copies links, devices, owners, groups and permissions from source to the target.

rsync command initially copies data from source to the target,there after it only copies the differences from source to the target.rsync is used to copy data on data disks and mirror the linux servers.

12 Linux rsync examples,must try it

12 Linux rsync examples,must try it

Example 1: Sync file on the local machine from one folder to another folder on the same machine.

[ SLTMachine: ] # rsync -zvh /tmp/myfile.txt /tmp/targetfolder/

Here,i am trying to sync myfile.txt to be available in the target folder.

  • -z is to enable compression
  • -v verbose ( print output/progress )

Example 2: Sync folder on the local machine to another folder on the same machine.

[ SLTMachine: ] # rsync -avzh /tmp/sourcefolder /tmp/targetfolder/

Here,i am trying to sync “sourcefolder” to be available in the target folder “targetfolder”.

Example 3: I want to transfer whole file,instead of sending only modified block.

By Default,rsync sends only new/modified block to target by doing checksum test.Due to this,sometimes,it will slowdown the copy of file from source to the target.

If you think,bandwidth is not at all problem to you.you can use the below option.It will send the complete file instead of only new/modified block.It will speedup the copy as we are avoiding the checksum test on the both sides.

[ SLTMachine: ] # rsync -avzw [email protected]:/test/sourcefile /tmp/targetfolder/

I am trying to copy the file from the remote server “220.220.220.32” to the local machine “/tmp/targetfolder”

Example 4: Will it delete the files at target location,if source doesnot maintain those files ? Yes.

Take scenario,where source doesnot have the files,but target has different files.In this scenario,rsync will the files from target location as it tries to sync/replicate the source folder

Use the following option to do the same.

[ SLTMachine: ] # rsync -avz --delete [email protected]:/test/sourcefile /tmp/targetfolder/

Here,if “/tmp/targetfolder” extra files against source location,it will be removed.

Example 5: Only modify the files at target location,dont create new files at target locaion in rsync.

I want to update only the existing files between source and target.But if source has new file,it should not create new file at the target location.

Use the following option to do the same.

[ SLTMachine: ] # rsync -avz --existing [email protected]:/test/sourcefile /tmp/targetfolder/

Example 6:Do not transfer files which are larger than 1000 Mega Bytes or some size.

[ SLTMachine: ] # rsync -avz --max-size=1000M [email protected]:/test/sourcefile /tmp/targetfolder/

It will not sync the files which are larger than 1000 Mega bytes.we can use “G” for Giga bytes and “K” for Kilo bytes.

Example 7: Include only one kind of files during the rsync.

[ SLTMachine: ] # rsync -avz --include '*.log' [email protected]:/test/sourcefile /tmp/targetfolder/

The above command only sync files which has .log extension.

Example 8: Exclude only one kind of files during the rsync.

[ SLTMachine: ] # rsync -avz --exclude '*.log' [email protected]:/test/sourcefile /tmp/targetfolder/

The above command will not sync files which has extenion “.log”.

Example 9: I want to know the differences between source and target location  like folders and files and permissions and file ownership.

[ SLTMachine: ] # rsync -avzi [email protected]:/test/sourcefile /tmp/targetfolder/

The above command checks the differences between source location and target location it will displays the differences.

Example 10: I want to see the progress of the rsync command.

[ SLTMachine: ] # rsync  -avz  --progress [email protected]:/test/sourcefile  /tmp/targetfolder/

the above command will show the progress of the rsync

Example 11: I want to do rsync between two folders everyday night at 23:00 .

30  23  *  *  *  rsync -avz [email protected]:/test/sourcefile  /tmp/targetfolder/

The above entry should be added in the crontab.So,crontab will take care of running the rsync command daily at 23:00.

Example 12: I want to synchronise only the directory structure not files.

[ SLTMachine: ] # rsync  -v -d  [email protected]:/test/sourcepath  /tmp/targetfolder/

the above command synchronises only the folder structure between sourcepath to the targetfolder

Thanks for reading the article on the “rsync” command.If you like it,please share it with others.If you want to add any new usage,please let me know in the comments section.i will add it here.Let everyone benefit out of it.

Please like us on “facebook,twitter,google+”.

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

Leave a Reply