You are on page 1of 6
‘2052023, 19:02 ‘1 rsyne command examples for Linux Beginners 11 rsync command examples for Linux Beginners Published on: March 24, 2018 by Pradeep Antil Reyne is a file coping tool that can copy files between a local system and remote system securely and efficiently. Rsyne is also widely used for backups and mirroring. Rsyne only needs to copy the differences between the systems that mean, let ‘say we have 2 directory names as directory and directory2. Now in directory! we have 2 files named as file] and file2, in directory? we have oniy one file ie filel so when we run rsync command to copy files from directory! to directory? then it will only copy file2 as filel already exists ‘As stated earlier rsync can be used to copy files locally or remotely, Let’s see the syntax to run them, Syntax to run rsyne locally is: ‘rsync options {source ie from where we want to copy} {destination i.e place where we want to copy} eg # rsync -av /directoryl directory2 Note: If destination directory is not created then it will be created automatically when we run rsync. Like here let say if we have not created directory2 but while running command we have mentioned destination as directory2 then it will be created. Syntax to run rsyne remotely: # rsyne options {source} {user’@host/path} ‘example is shown below, # rsync -av /directory1 student@server1.example. com: /directory2 ‘There are many advantages of rsyne, let see few of them: Reyne copy files securely from one system to another. It is very versatile file coping tool, it has many options that can be used while coping. It do not need to have super-user privileges in order to use it, tis much faster way to copy files. Tf while coping files over the network, if network gets disconnected by any reason the coping start from where it was stopped due to network issue not like sep that it will start coping from beginning. ‘+ Insync we have option to compress and then copy the files hence increasing the speed and it takes less time to copy. ‘Now let see few example of rsyne: ‘ntps sw inuxbuzz.comsyne-command-examples-tnux-beginners! 16 240512023, 13:02 ‘1 rsyne command examples for Linux Beginners Before we proceed here in most of the examples we have taken 2 directories that are directory! and directory2. In directory! wwe have 3 files named as filel, file2 and file3 while directory? is empty. Example:1 Copy/Transfer data in a local machine wwe can use rsyne command to copy/transfer data in our local machine from one destination to other. [root@desktop ~]# rsync -av directory1/ directory2/ sending incremental file list av filer file2 File3 sent 245 bytes received 72 bytes 634.00 bytes/sec total size is 55 speedup is 0.17 Example:2 rsyne dry run Lets assume we are beginner and we don’t know that what will happen if we run the rsyne command, In that case we need not to worry there is an option in rsyne which we can use to check what happens when the command really gets executed. ‘The option used is “-n”. After checking the output if we get desire result then we can run the command without option “1 [root@desktop ~]# rsync -vn directory1 directory2 skipping directory directory1 sent 8 bytes received 12 bytes 40.00 bytes/sec total size is @ speedup is 0.00 (DRY RUN) + -vis for verbosity to the output as coping proceed. ‘+ -mis used for dry run. Example:3 Archiving mode in rsync command via option -a syne do not preserve permission and timestamps of the file while coping. In order to that we need to use option ~a, It allows copying of files recursively in archiving mode and it also preserves symbolic links, file permissions, user & group ‘ownerships and timestamps. ntps:iwwwwtnuxbuzz comirsyne-command-examples-Inux-beginners! 26 ‘2052023, 19:02 ‘1 rsyne command examples for Linux Beginners [root@desktop ~]# rsync -av directory1/ directory2/ sending incremental file list af filer File2 File3 sent 245 bytes received 72 bytes 634.00 bytes/sec total size is 55 speedup is 0.17 Example:4 Compressing files during copy/transfer in rsync Let assume if files that we need to copy are too large then we can compress the file by using option -z [root@desktop ~]# rsync -azv directory1/ directory2/ sending incremental file list : filer file2 file3 sent 233 bytes received 72 bytes 610.00 bytes/sec total size is 55 speedup is 0.13 Example:5 Coping/Transfer data from local machine to a remote machine In above example which we have seen in that we have copied files locally, now let's try to copy files to remote system using syne command [root@desktop ~]# rsync -avze ssh /var/log root@192.168.122.1:/tmp sending incremental file list log/ og/Xorg.0.1og og/Xorg.0.10g.0ld 1og/Xorg.1.log 1og/Xorg.9.10g log/boot . log log/btmp tps: tnuxbuzz comisyne-command-examples-Inux-beginners! 36 ‘2052023, 19:02 ‘1 rsyne command examples for Linux Beginners sent 1116324 bytes received 1760 bytes 131539.29 bytes/sec total size is 11511054 speedup is 10.30 Example:6 Coping/Transfer data along with specific protocol (option -e) If we want to use rsyne command with any specific protocol then we need to use option -e Like here we have used “ssh” where we can take remote session of machine by entering the user password, ‘Then we would like to proceed with coping of files [root@desktop ~]# rsync -avze ssh /var/log root@192.168.122.1:/tmp eee ae ape eae Cee a Co ECDSA key fingerprint is 6c:43:1e:b7:8a:8d:82:d4:c2:7¢:ac:43:Fd:d4: Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.122.1' (ECDSA) to the list of known hosts. root@192.168.122.1's password: sending incremental file list log/ 1og/Xorg.@.10g Log/Xorg.@.1og.old 1og/Xorg.1. 10g log/Xorg.9.10g Log/boot .1og log/btmp Log/btmp-26180309 log/cron 1og/cron-26171206 Log/dnesg, Log/dnesg.old log/lastlog Log/maillog Log/mai llog-20171206 sent 1116324 bytes received 1768 bytes 131539.29 bytes/sec total size is 11511054 speedup is 10.30 Example:7 rsync include and exclude option (—include , -exclude) ntps:iwwwwtnuxbuzz comirsyne-command-examples-Inux-beginners! 406 ‘2052023, 19:02 ‘1 rsyne command examples for Linux Beginners Let's assume that we want to copy only txt files and we want to exclude all other files, then in this situation we can use — inchude/ -exclude option. [root@desktop ~]# rsync -azv --include ‘*.txt’ --exclude '*' directory1/ directory2/ Example:8 Set Max file size during copy or transfer in rsync command (— max-size) To set the max size of files that need to be transferred, there is an option in rsync where we can define the maximum size of file that we want to transfer and the option for itis “-max-size”, (-max-size~"mention the size of file”), example is shown below, [root@desktop ~]# rsync -avz ~max-size=5@@k /var/log /tmp Example:9 View the progress of data transfer in rsyne command (-progtess ) Ifyou want to see how much time is left for complete transfer of files then we can use “progress” option. [root@desktop ~]# rsync -avz --progress /var/log /tmp ntps:iwwwwtnuxbuzz comirsyne-command-examples-Inux-beginners! 516 2ayos7023, 1902 ‘1 syne command examples for Linux Beginners Example:10 Set bandwidth limit during copy/transfer File (-bwlimit={set bandwidth limit}) ‘There is an option in rsync where you can even set bandwidth limit while transferring file. [root@desktop ~]# rsync -avze --bwlimit=200 /var/log /tmp Example:11 Automatically deleting the source files after successful copy/Transfer ‘There situations where we don’t want to keep the files/data in source after transferring it to destination, In that case we can use option “-remove-source-files” [root@desktop ~]# rsync -azv --remove-source-files directory1/ directory2/ Above mentioned are few examples of rsyne commands. There are many more options, Try and play with this command its very useful command for syncing/coping/transferring the data ntps:iwwwwtnuxbuzz comirsyne-command-examples-Inux-beginners! a6

You might also like