Rsync (Remote Sync) is the most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems.
With the help of the rsync command, you can copy and synchronize your data remotely and locally across directories, disks, and networks, perform data backups, and mirror between two Linux machines.
This article explains 16 basic and advanced uses of the rsync command to transfer your files remotely and locally in Linux-based machines. You don’t need to be a root user to run the rsync command.
Table of Contents
Advantages of Rsync Command
It offers several advantages, including:
- Efficient File Transfer – rsync uses a delta transfer algorithm, which means it only transfers the differences between source and destination files, which significantly reduces the amount of data transferred, making it efficient for syncing large files or directories.
- Remote File Synchronization – rsync supports both local and remote file transfers over SSH, which allows synchronization between local and remote systems or mirroring directories across multiple machines.
- Incremental Backups – rsync is well-suited for incremental backups, as it create and update backups efficiently by transferring only new or modified files.
- Preserves File Permissions – rsync can preserve various file attributes, such as permissions, ownership, timestamps, and symbolic links, which ensures that the copied files retain their original characteristics on the destination.
- Bandwidth Control – rsync allows you to limit the bandwidth usage during file transfers, as it uses compression and decompression method while sending and receiving data on both ends.
- Faster – rsync can be faster than scp (Secure Copy) for transferring files, especially when syncing large directories or when dealing with files that have already been partially transferred or exist on the destination.
Rsync Command Syntax
The rsync command follows the following syntax:
# rsync [OPTIONS] SOURCE DESTINATION
Here’s an explanation of the different components and options used with rsync commands:
-v
– Verbose output, displaying detailed information about the transfer.-r
– copies data recursively (but doesn’t preserve timestamps and permission while transferring data.-a
– archive mode, which allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships, and timestamps.-z
– Compress files during transfer to reduce network usage.-h
– human-readable, output numbers in a human-readable format.-P
– Show progress during the transfer.SOURCE
– Specifies the source file(s) or directory to be transferred, which can be a local or a remote location.DESTINATION
– Specifies the destination path where the files or directories will be copied. Similar to the source, it can be a local path or a remote location.
Install Rsync in Linux System
We can install the rsync package with the help of the following package manager as per your Linux distribution.
$ sudo apt install rsync [On Debian, Ubuntu and Mint] $ sudo yum install rsync [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] $ sudo emerge -a sys-apps/rsync [On Gentoo Linux] $ sudo apk add rsync [On Alpine Linux] $ sudo pacman -S rsync [On Arch Linux] $ sudo zypper install rsync [On OpenSUSE]
1. Copy/Sync File Locally
To copy or sync a file locally, you can use the following command that will sync a single file on a local machine from one location to another location.
Here in this example, a file name backup.tar needs to be copied or synced to /tmp/backups/ folder.
[root@tecmint]# rsync -zvh backup.tar.gz /tmp/backups/ created directory /tmp/backups backup.tar.gz sent 224.54K bytes received 70 bytes 449.21K bytes/sec total size is 224.40K speedup is 1.00
In the above example, you can see that if the destination is not already existed rsync will create a directory automatically for the destination.
2. Copy/Sync Directory Locally
The following command will transfer or sync all the files from one directory to a different directory in the same machine.
Here in this example, /root/rpmpkgs contains some rpm package files and you want that directory to be copied inside /tmp/backups/ folder.
[root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm sent 3.47M bytes received 96 bytes 2.32M bytes/sec total size is 3.74M speedup is 1.08
3. Copy a Directory from Local to Remote Server
To copy a directory from a local server to a remote server, you can use the following command, which will sync a directory from a local to a remote machine.
For example, if there is a folder in your local computer “rpmpkgs” that contains some RPM packages and if you want that local directory’s content sends to a remote server, you can use the following command.
# rsync -avzh /root/rpmpkgs [email protected]:/root/ The authenticity of host '192.168.0.141 (192.168.0.141)' can't be established. ED25519 key fingerprint is SHA256:bH2tiWQn4S5o6qmZhmtXcBROV5TU5H4t2C42QDEMx1c. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.0.141' (ED25519) to the list of known hosts. [email protected]'s password: sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm sent 3.74M bytes received 96 bytes 439.88K bytes/sec total size is 3.74M speedup is 1.00
4. Copy a Directory from Remote to Local Server
This command will help you sync a remote directory to a local directory. Here in this example, a directory /root/rpmpkgs which is on a remote server is being copied into your local computer in /tmp/myrpms.
# rsync -avzh [email protected]:/root/rpmpkgs /tmp/myrpms [email protected]'s password: receiving incremental file list created directory /tmp/myrpms rpmpkgs/ rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm sent 104 bytes received 3.49M bytes 997.68K bytes/sec total size is 3.74M speedup is 1.07
5. Rsync Over SSH
With rsync, we can use SSH (Secure Shell) for data transfer, using SSH protocol while transferring our data you can be ensured that your data is being transferred in a secured connection with encryption so that nobody can read your data while it is being transferred over the wire on the internet.
Also when we use rsync we need to provide the user/root password to accomplish that particular task, so using the SSH option will send your logins in an encrypted manner so that your password will be safe.
To use rsync over SSH, you can use the -e
option to specify the remote shell command, which is typically ssh as shown.
# rsync [OPTIONS] -e ssh /path/to/source user@remote:/path/to/destination
6. Copy a File from a Remote Server to a Local Server with SSH
To synchronize a file from a remote server to a local server, you can specify a protocol with rsync using the “-e” option with the protocol name you want to use.
Here in this example, We will be using the “ssh” with the “-e” option and perform data transfer.
# rsync -avzhe ssh [email protected]:/root/anaconda-ks.cfg /tmp [email protected]'s password: receiving incremental file list anaconda-ks.cfg sent 43 bytes received 1.10K bytes 325.43 bytes/sec total size is 1.90K speedup is 1.67
7. Copy a File from a Local Server to a Remote Server with SSH
To synchronize a file from a local server to a remote server using SSH, you can leverage the following command as shown.
# rsync -avzhe ssh backup.tar.gz [email protected]:/backups/ [email protected]'s password: sending incremental file list created directory /backups backup.tar.gz sent 224.59K bytes received 66 bytes 64.19K bytes/sec total size is 224.40K speedup is 1.00
8. Show Progress While Transferring Data with Rsync
To show the progress while transferring the data from one machine to a different machine, we can use the ‘--progress'
option, which displays the files and the time remaining to complete the transfer.
# rsync -avzhe ssh --progress /root/rpmpkgs [email protected]:/root/rpmpkgs [email protected]'s password: sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm 1.47M 100% 31.80MB/s 0:00:00 (xfr#1, to-chk=3/5) rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm 138.01K 100% 2.69MB/s 0:00:00 (xfr#2, to-chk=2/5) rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm 2.01M 100% 18.45MB/s 0:00:00 (xfr#3, to-chk=1/5) rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm 120.48K 100% 1.04MB/s 0:00:00 (xfr#4, to-chk=0/5) sent 3.74M bytes received 96 bytes 1.50M bytes/sec total size is 3.74M speedup is 1.00
9. Include Files with Particular Extension with Rsync
To include specific files or patterns during a rsync operation, you can use the --include
option with an extension that matches all files.
# rsync -avz --include='*.txt' /path/to/source/ user@remote:/path/to/destination/
In the provided example, rsync will include only files with the .txt
extension from the /path/to/source/ directory during the transfer.
10. Exclude Files with Particular Extension with Rsync
Similarly, to exclude a specific extension during a rsync operation, you can use the --exclude
option with a wildcard pattern.
# rsync -avz --exclude='*.ext' /path/to/source/ user@remote:/path/to/destination/
In the provided example, rsync will exclude files with the specified extension (*.ext)
during the transfer, while including all other files and directories.
11. Include and Exclude Files with Rsync
To include and exclude specific files or patterns during a rsync operation, you can use both the --include
and --exclude
options with appropriate wildcard patterns.
These two options allow us to include and exclude files by specifying parameters these option helps us to specify those files or directories which you want to include in your sync and exclude files and folders with which you don’t want to be transferred.
Here in this example, the rsync command will include those files and directories only which starts with ‘R’
and exclude all other files and directories.
# rsync -avze ssh --include 'R*' --exclude '*' [email protected]:/var/lib/rpm/ /root/rpm
12. Use of –delete Option with Rsync
If a file or directory does not exist at the source, but already exists at the destination, you might want to delete that existing file/directory at the target while syncing.
We can use the ‘--delete
‘ option to delete files that are not there in the source directory.
The source and target are in sync. Now create a new file test.txt at the target.
[root@tecmint:~]# cd /root/rpm/ [root@tecmint:~/rpm]# touch test.txt [root@tecmint:~/rpm]# rsync -avz --delete [email protected]:/var/lib/rpm/ /root/rpm/ [email protected]'s password: receiving incremental file list deleting test.txt ./ .dbenv.lock .rpm.lock Basenames Conflictname Dirnames Enhancename Filetriggername Group Installtid Name Obsoletename Packages Providename Sha1header Sigmd5 Suggestname Supplementname Transfiletriggername Triggername __db.001 __db.002 __db.003 sent 445 bytes received 18,543,954 bytes 2,472,586.53 bytes/sec total size is 71,151,616 speedup is 3.84
Target has the new file called test.txt when synchronizing with the source with the ‘–delete
‘ option, it removed the file test.txt.
13. Set File Transfer Limit with Rsync
You can specify the Max file size to be transferred or synced. You can do it with the “--max-size
” option. Here in this example, the Max file size is 200k, so this command will transfer only those files which are equal to or smaller than 200k.
# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ [email protected]:/root/tmprpm [email protected]'s password: sending incremental file list created directory /root/tmprpm ./ .dbenv.lock .rpm.lock Conflictname Enhancename Filetriggername Group Installtid Name Obsoletename Recommendname Requirename Sha1header Sigmd5 Suggestname Supplementname Transfiletriggername Triggername __db.002 sent 129.52K bytes received 396 bytes 28.87K bytes/sec total size is 71.15M speedup is 547.66
14. Automatically Delete Source Files After Transfer
Now, suppose you have the main web server and a data backup server, you created a daily backup and synced it with your backup server, but now you don’t want to keep that local copy of the backup in your web server.
So, will you wait for the transfer to complete and then delete that local backup file manually? Of Course NO. This automatic deletion can be done using the ‘--remove-source-files
‘ option.
# rsync --remove-source-files -zvh backup.tar.gz [email protected]:/tmp/backups/ [email protected]'s password: backup.tar.gz sent 795 bytes received 2.33K bytes 894.29 bytes/sec total size is 267.30K speedup is 85.40 [root@tecmint:~]# ls -l backup.tar.gz ls: cannot access 'backup.tar.gz': No such file or directory
15. Do a Dry Run with Rsync
If you are a newbie using rsync and don’t know what exactly your command going to do. Rsync could really mess up the things in your destination folder and then doing an undo can be a tedious job.
Use of this option will not make any changes to the files and shows the output of the command, if the output shows exactly the same as you want to do then you can remove the ‘--dry-run
‘ option from your command and run on the terminal.
# rsync --dry-run --remove-source-files -zvh backup.tar.gz [email protected]:/tmp/backups/ [email protected]'s password: backup.tar.gz sent 50 bytes received 19 bytes 19.71 bytes/sec total size is 267.30K speedup is 3,873.97 (DRY RUN)
16. Rsync Set Bandwidth Limit and Transfer File
You can set the bandwidth limit while transferring data from one machine to another machine with the help of ‘--bwlimit
‘ option. This option helps us to limit I/O bandwidth.
# rsync --bwlimit=100 -avzhe ssh /var/lib/rpm/ [email protected]:/root/tmprpm/ [email protected]'s password: sending incremental file list sent 324 bytes received 12 bytes 61.09 bytes/sec total size is 38.08M speedup is 113347.05
Also, by default rsync syncs changed blocks and bytes only, if you explicitly want to sync the whole file then you use the ‘-W‘ option with it.
# rsync -zvhW backup.tar /tmp/backups/backup.tar backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10
Conclusion
That concludes our overview of rsync and its capabilities. For further exploration of its extensive options and functionalities, I encourage you to refer to the comprehensive manual pages (man pages) available.
What terminal is it? Colorful, great looking. The first terminal screenshot. Greens, reds, etc.
@aki,
Try adding the following code to .bashrc file of user, logout and login back to get colorful terminal prompt.
export PS1="\[\e[32m\][\[\e[m\]\[\e[31m\]\u\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\]:\[\e[36m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\[\e[32;47m\]\\$\[\e[m\] "
Rsync on Linux Excellent…
For rsync between two Linux machines, I have to install rsync on both.
Then do I have to somehow run rync on the receiving machine as a daemon? so that it can wake up and handle the incoming connection? If so, how do I get it running as a daemon? just rysnc& ??
Hi,
I want to copy files from remote Linux system to my laptop using SSH client. I use XShell for ssh.
Now in my local machine i.e. laptop (Windows10), the ‘rsync’ is not installed. So, when i typed the command “rsync -avzh [space]e:” it says ‘rsync’ not recognised.
so. my question is
@kkm,
Use the WinSCP windows tool and connect to a remote Linux system and transfer files to your local windows.
Use screen command
Hi, How to run a Rsync in a Background? I have tried using nohup but it stopped.
@Dinesh,
You can add
&
at the end of command while running rsync.How to send multiple files in rsync?
@Dinesh,
These commands will help you…
Hi
The command similar to the suggested:
through’s error – The source and destination cannot both be remote.
rsync error: syntax or usage error (code 1) at main.c(1285) [Receiver=3.1.3]
the error meaning is straight forward but, I am giving this command where I access Linux through ‘ssh’ and gave this command. Does this make difference?
Trying to back up a VPS on a home computer running Windows. As I understand this will be done via SSH. Using Backup Agent 6.65 when putting in VPS address it requests I insert the proper volume. I either have to do more to sync it or there is an error in the path to the VPS.
How do I prove the path to get the volume to be recognized or otherwise synchronize with everything? Or if I need to start from scratch with another service, what should I use? Thank you.
Thanks a lot
Hi,
Is there any option to use rsync command with http URL? I want to sync my local repo with remote maven repo.
I have tried
rsync -ahzv http://10.20.25.23:8081/mr/ds/ /home/.m2/repository
but it didn’t work for me.Could you please help me on this?
@Srinivas,
Rsync won’t work with HTTP URL..
Thank you Ravi..
You can use reposync
What about daemon mode?
Nobody explains, besides running under inetd and configuration files, how to actually use with source and destination.
I’d like to copy an external hard drive (iTunes Library, etc.) to another external hard drive (to serve as a back-up) when they are both connected to my MacBook Pro. I am not a programmer and have no experience or understanding of the codes and commands you all may take for granted.
A friend told me about rsync and I have done a little exploring online. It would be so helpful if someone would provide the script that I could copy-and-paste into the Terminal window.
@Clay,
First attach your two external hard drives to system and then find out device names, you can use this article to find device names: https://www.tecmint.com/find-usb-device-name-in-linux/.
Once you know your device name, you can simply run rsync command from the terminal as follows:
How to auto rsync when i upload to master server complete.
What about rsync and hidden files in one pass. Is this possible?
I used rsync for ./Leslie/* and then for ./Leslie/\,*
I did not notice anywhere where Icould combine the two rsync commands to save one pass
I think you might to enter both arguments one after one as it can be done with cp or mv tools but only difference that it must be put in brackets like this:
'./Leslie/* ./Leslie/.*'
.I want to point out that you should definitely NOT use the
-z
option (=compress) when using rsync for a local transfer.This will unnecessarily slow things down.
(check out e.g. https://stackoverflow.com/questions/43624832/rsync-compression-and-how-it-works)
Hi, its good stuff, I need to automate following job process, I need to do same for every one hour, I feel its better to make use of rsync, but how to use this for my application? I can not do any thing at remote server side. i got sftp access only, generally I am doing following things manually
step1:
step2:
step3:in above directory every day one directory will create, in this directory some files are dropping.
step4:
I need to automate this
I need to rsync two machines losing connectivity as an standard. I mean they only see on a short period of time and i can’t know when that happens. have rsync some exit code? Or must i just use an script and try one and another time until they see? Any timeout setup to allow the search to bee short?
Best regards.
@Donald,
If it’s a connection timeout because of SSH server is slow to respond, you can set the timeout option while running rsync as shown.
Hello friends,
Can any one tell me how to keep update my backup machine.
The problem is here.
Source to destination synchronization need to be 100% accurate. Actually I have 2 servers that I need to be sync (100% copy of each other). I have tested it in the following way.
rsyncdemo dir on both servers that I need to synchronize that contains further sub folders and files.
Test 1:
It copies all data successfully no issues. but problem is that if I delete a file or folder on the source machine it do not delete it from the destination.
Test2:
Hi,
I am trying to use the rsync with
--delete
(step :6). But unable to remove the files in the destination.How do I use rsync to copy incremental files from window server to my linux server. Need to use sftp.
Why I am getting 1 hour time difference from the original source file, whereas both target and source machine have the same times.
@Fasih,
It could be system time mismatch, set both server time to similar and see..
see whether executing “date -u” on both sides still show the same times.
I’m using rsync, talking to a rsyncd, to avoid encryption through ssh, as the rsyncing machines are low powered embedded boards. Consequently, the rsyncd provides modules to address the server directories, say, a module “backup”.
A client machine now attempts to sync some local directories to a corresponding path under the directory, specified by module on rsyncd machine, like:
But as long as $path doesn’t exist on the server, rsyncing $dir fails. I’m looking for a way to create $path on the server automatically when written to, akin to what mkdir -p does. Given that the canonical path on server is hidden behind the module name, letting client ssh to a server in order to create the directory prior to rsync is something I want to avoid.
I haven’t found an rsync option allowing the creation of destination directories on a needs base. Do you have any suggestion how to solve this case, short of manually creating destination directories prior to first use?
I was thinking of some intricate scheme of iterating through parent directories, rsyncing each level of parent directories while excluding all siblings until decided to the source directory. While this could possibly work (untested), I doubt that this effort is actually meant to be necessary for this presumably rather common use case.
Thank you for any suggestion.
I’m now considering a refinement to the thought of method above: Rather than iterating through directories, rsyncing each level separately for the creation of a single directory level, I’m now considering to try creating the whole path in a temporary directory, rsyncing all of those in one go, then removing the temporary directories again.
In consequence, the whole path has been created on the server, ready to commence the actual data transfer to its deepest level directory. Less effort already, but still rather Rube Goldbergish – more ideas still welcome.
I’ve now, at least for the time being, settled for the described approach:
* creation of temporary directory (mktemp -d)
* creation of wanted path in temp directory (mkdir -p)
* rsync top level directory of wanted path from temporary directory (rsync – this creates wanted the directory hierarchy on server)
* removal of temporary directory (rm -r)
* rsync $path dest::module/$path # as destination directory already exists, rsync proceeds without failure.
while probably not the most elegant method, the additional complexity is manageable: no iterating through path components, or other path decomposition. just plain use of path components already existing for scripting purposes. So unless a better (working) idea pops up, I’m happy for now. Thank you all for bearing with me.
The trick is to put trailing slashes on the paths:
Directory “source” will be named “target” at the receiving system. Provide the exact name you want.
Actually, there is a trailing slash in the source path, which got hidden behind using the variable $dir. A copy and paste of an actual and complete call are here (well, not quite actual, I simplified options a bit, but the result is the same):
Using or not using a trailing slash makes no difference here. rsync succeeds as soon the directory vpn/home/l/read is manually created under the directory representing module backup. This is the case regardless whether the destination is specified by module name or by canonical name over ssh.
Another thought came up which I may have to try out: Exclude everything by option, then include the topmost directory of the wanted path. Current “solution” is still to create destination directories manually when necessary.
Regarding about my previous comment: “then include the topmost directory” won’t do, as this transfers the unwanted siblings too. The whole path, down to deepest source directory, should be needed.
I don’t know why it differs for you. I have no trouble with this and I do it all the time (Linux). Here’s one I just used last weekend, worked like a charm.
Hi,
If I set rsync the folder of the files will get automatically change or need to set some cron for that? Please help me out.
@Vignesh,
To auto sync files/folders you must set cron for rsync.
You could have a look at lsyncd, which watches for modification on files through inotify, then transfers the modified files using rsync upon modification.
An alternative could be incrontab to do essentially the same. Latter is a more general approach, as incrontab can be used for other actions than rsync. Both methods will allow you to monitor changes and rsync those automatically shortly after they took place.
It seems comments get edited prior to being published. I noticed earlier that words were edited, unfortunately introducing errors that way. Same here: I originally wrote “incrontab“, not “in crontab”. While cron merely executes by time specification, incrontab allows inotify event specification, which is what I was actually referring to: https://linux.die.net/man/1/incrontab
@Paul,
Yes, comments were edited only if needed, but sorry I mistook it incrontab as “in crontab“. Thanks for clarification about incrontab, never heard before.
Thanks you for correcting my original message another time. Please change also:
“An alternative could be crontab to do essentially the same. Later is a more general approach, as in crontab can be used for other actions than rsync”
to
“An alternative could be incrontab to do essentially the same. Latter is a more general approach, as incrontab can be used for other actions than rsync”
In earlier messages, I was several times wondering already why I have overlooked evidential mistakes when rereading prior to sending – I initially thought that some autocorrection went wrong, and have the impression that editing introduces more mistakes than it fixes.
@Paul,
Sorry for trouble, corrected the line in the last comment as suggested..
Hello, is there any way to produce an exit code if it sees that source and destination are not the same while using the option –dry-run? Thanks
@Jeff,
I think you should read this article, that shows how to sync only new or changed files over rsync.
https://www.tecmint.com/sync-new-changed-modified-files-rsync-linux/
When rsyncing from local to local, compressing only slows it down. Skip the -z option if you’re backing a local disk up to a local disk, because it’s just reading the whole file, compressing it, decompressing it, and writing the whole file. No need for the compression middleman if the sync isn’t over a network.
@Bill,
Thanks for the tip, didn’t know this, actually I never used rsync for local backups, I always used to sync remote servers with local, anyway thanks hope this will help other users who used to backup files locally..
Hi Ravi,
I have tried as per your suggestion but it’s not working as my requirement. still, am able to copy the timestamp which has presented on the source .
Thanks for your reply.
As per your above command, it doesn’t meet my requirement. I guess ‘n’ argument is dry-run (not do any file transfers, instead it will just report the actions it would have taken.)
Please help me on this and I need only copy changed folders in source and don’t want copy the time stamp which is not copied and changed on destination.
Hi,
With Rsync command I want only copy/sync changed files/folder to the destination folder. I had some issue with Rsync-like whenever am executing the Rsync command am copied along with the time stamp as well.
For example, my destination folder ‘Linux’ had updated on 30th June and in my source folder there is no update info for the Linux folder, but when am performing the Rsync command my destination Linux folder time stamp has been updated with source folder time stamp. I don’t want to copy the timestamp as well. please suggest me on this and please glance on below command which I have used.
rsync -avh /source/Linux/ /destination/Linux/
@Raja,
You can use following command to only sync new or changed files over rsync to destination folder:
Thanks for your reply.
As per your above command, it does not work because the ‘n’ argument tells the dry-run (not do any file transfers, instead, it will just report the actions it would have taken)
For your info am doing this Rsync with two directories present on the same server. Please help me on this.
Simply, need to copy only latest changed files/folders from source to destination.(don’t want to copy the timestamp which has not changed in source)
@Raja,
Yes, the
-n
option is used to check the files, and once you confirm that the files are listed correctly on the dry-run, remove the -n option and run:Hi Ravi,
I have tried as per your suggestion but it’s not working as my requirement. still, am able to copy the timestamp which has presented on the source .
Thanks for your reply.
As per your above command, it doesn’t meet my requirement. I guess ‘n’ argument is dry-run (not do any file transfers, instead it will just report the actions it would have taken.)
Please help me on this and I need only copy changed folders in source and don’t want copy the time stamp which is not copied and changed on destination.
Hi Ravi,
You mentioned in one of your examples that you could transfer the contents from source to destination securely over ssh when you use the “-e” option and specify ssh. May i know that over what protocols is the transfer done when not using ssh option?
@Gaurav,
Yes, I do mentioned that you can transfer files from source to another securely using SSH, but still it all depends on which protocol you configured under sshd_config file, I suggest to use Protocol 2 for more better security in SSH configuration file..
rsync can talk to remote rsync daemon without tunneling through ssh
rsync works great but how do I get it to keep using the entire network bandwidth for really large files, 1tb+? I’m hitting 200mbs a sec over a 10gb ethernet connection in the beginning but sometimes it slows to 20mbs for no apparent reason and stays there.
Is there a way to have it check or do an ack reset to re-negotiate the link speed through the switch?
@Daniel,
The slowness happens because of file encryption during transfer files over SSH, if you have that much of large data, you can reduce the encryption level or use other alternative tool like parsync (a rsync wrapper for larger data transfer).
@Daniel, for the same reason, do I now run rsync as daemon server side, launched through xinetd. Now rsync can talk to rsyncd without ssh, and throughput went up considerably, while CPU load dropped.
Security is of no concern in my case, as transfers take place in a trusted LAN, or go through a VPN – in which case I merely avoided double encryption, by SSH in addition to by VPN).
A new problem was then introduced though, for which I have currently no proper solution – I wrote about this in another post just a few minutes ago in another comment here, asking for suggestions.
@Daniel, regrettably I avoided addressing your actual issue: bandwidth limiting. Well, that’s possible with rsync running as daemon too, by use of the option –bwlimit=RATE. through this setting can you control the maximum amount of data transferred per time unit.
Thank you for the post.
May I ask you if you may know how to setup a directory sync between two servers while using daemon running under /etc/xinetd.d/rsync.
I target not to use just a command like in your examples, but to have xinetd running rsync in the background.. Samba.org examples of rsyncd.conf are not clear how to setup the path and the destination servers.
Also in /etc/xinetd.d/rsync configuration there is no reference thus it is not clear if rsyncd.conf can be relevant and used at all. So how to make only two specific servers setup this way where one will be a source and another a destination, meaning that the content will be actively changing on one server, while another will get its folder just synced with the first one.
@Mark,
Sorry, no idea never thought of running rsync under xinted, will certainly try and give solution if it works successfully
Nice and relevant examples.
Hi, really its very helpful .. Thanx for this article
Hi, I want to copy the files from one location to another over WAN. both locations are in two geographical locations. Is it possible to achieve it through RSYNC ??
@Dash,
Of course yes, rsync will work with any network even both machines are on different network and far from each other, rsync works well.
how the source and destination paths are going to be mounted then ?
@Dash,
Rsync is used to sync files between two servers or transfer files from local to remote and vice-versa, it’s not meant for mounting. Could you explain me more about your questions? so that I can help you more better way..
Thank you! Really well explained.
Nice post.
Thanks You very helpful commands for beginners like me but I am looking for copying files using rsync command from one server to another server directly which I did not get. If anybody have please share .
Under RHEL 5.0, there is no argument called “–remove-source-files” instead there is a option called “–remove-sent-files”
For example: If you are trying to copy the file to a local or remote system, you would use the following command.
$ rsync –remove-sent-files -avzh /tmp/aziz.tar root@localhost:/tmp/backup1/
Note: While specifying the destination directory, if the directory is not proceeding with ‘/’ it will create a file called /tmp/backup1. We need to ensure the directory ends with ‘/’ if we want to create the destination directory while runnning rsync.
Hope it helps
@Bhasker,
Thanks for the tip, hope it will help others…
Thanks for this tutorial. I am a beginner in the server world and i find this quite useful.
I have some questions and if you can answer that would be great.
I need to transfer data from one remote server to another.
rsync -av [email protected]:/var/www/something [email protected]:/var/www/something
is this going to work?
also is there any option to run rsync command and then log out from my console (leaving rsync in progress). I need to transfer around 150GB and it would be great if i dont have to leave my terminal on.
Kind regards
@Kirba,
I never tried that way and I am not sure it will work or not, I think you should give a try and see how it works and to keep the session active better run via cron..
Thanks i will try to setup cron
Hi, tmux is a great tool for this (as well as screen), for detaching from a running process/terminal and reattaching later. also nohup command may be useful but not as versatile for this purpose, as it demonizes your process and writes to a log file
I am new to linux. This site is Really useful..!! Thankx
Hi, I have a request where I have to copy a directory(all of it’s files and sub directories inside it) to a destination. The destination will only have the top level directory. Basically, can I copy directories instead of files with rsync? If Yes, what happens if I run the same script with same ‘source’ and ‘destination’ again? Will it override what’s already exist in the destination directory?
Thanks,
Shahul
very useful post, but i have a question, i tried to send some files with spaces in the name, taking the filenames from varaibles, and don`t work, what can i do???
p.d. you’re cute…
@Emmanuel,
May be you’re wrongly mentioning files names. For example, the ‘Linux ISO’s’ directory should be called like this:
So, call filenames with spaces same way as I showed above during rsync..
Just want to confirm my understanding. If I issue the command from my source Linux server, “rsync -avzhe ssh /home/dir/ root@destn_server:/backup_Directory/”, all the directories and files of my source Linux Server under /home/dir/ would be copied to the destination server ‘destn_Server’ under ‘/backup_directory’ with the compression done on the fly. Is that correct ?
@Isaac,
Yes you correct it will archive and copy all the files and folders with hard links from source to destination directory on remote server.
Thanku it”s great job done i am really appreciate to you
thanks
Good Info.. Thanks for sharing ! :)
Really good, brief and clear!
A big Thank you! :)
Very useful article which covered most of the important rsync options.
Can you please add and elaborate on -P or –partial option which deals with partial progress/resume capability?
@Siddhit,
Your request is accepted, I will try to add one more point to the article on -P or -partial option, just stay tuned…
Great job! Very useful article!
Thank You!
Just what I needed. Awesome !!!
Very good article..
helpful command examples..
Thank u very much
Thanks a lot for this wonderful article! <3 tecmint, keep sharing good stuff (y)
Thank you for the article. Its quite beneficial over a man page. Cheers.
Thanks very usefull !!!
Thanks alot
Thanks so much – very handy!
try KrojamSoft SyncFile its also worth using…
Thanks a bunch, Tarunika. You saved me an entire day’s work with your article.
Hi
Thanks a lot for this article.
It is written in a clear and understandable manner.
I already set up a script to back up mails on a regular basis and it works.
hi,it’s a good one.
gud luck.
Want to use Rsync for process.
To be more specific, I have a process where only designated files need to be transferred from a remote server to the local server. The local server will be will be fed a continual list (day-to-day) of files that need to be transferred from the remote server. Once these files are transferred to the local server, they do not need to be sync’d with the remote server. Any changes to the original file (remote server), would require another version release, thus a different file name.
I am looking to be able to run a script on the local server that would receive the file input list, then compare against the files currently on the local server to see if resident or not. If file not on local server, transfer file from the remote server.
Thanks for any help on this.
Can Rsync be used for only copying files from a remote server to a local server and not sync the files? Don’t want to have continual file copy traffic.
Just need to manage specific files transfer based on internal designation of those files that need to be copied.
The rsync tool works on both sides, remote-to-local and local-to-remote. But sorry, your question not clear to me..can you explain more cleary exactly what you want to do.
Excellent article, non unix/Linux admins also easily can understand with these good examples.
Very Nice
From PARIS (FRANCE)
Hi
I have the link dest and target folder on remote desktop – when I run rsync ssh on the command line it works fine but in script I try to put in the link dest folder relative to the target and it wont work ie -link-dest=../../link – what am I doing wrong
Howard
Can you show the command you are using to do.
Hi
If i want sync folder in daily day at 3:00 PM
what i do ?
Simple, set a cron for it.
Hi Tarunika,
How can we copy the latest log file with rsync from remote server to our local dir?
Hi Taru (Stars in our Language ;))
Thank you for the post. it was really informative. My question is how do I synchronise Source Folder & a Target folder? Source Folder is been in Ubuntu and the Target folder is in Windows BUT mapped in to the Source Server as a local folder. When I use freesync it only copies the folders but not the contents. Push out an error stating file failed to sync cos its not permitted :( NOW when i do a basic copy and paste everything gets copied. Now I do not want to be doing that like a prayer would I ? So any help/Idea?. Thanks for your help in advance…..Have a good one
this is awesome, thank you very much for breaking down the benefit of Rsync for us :)
Really understandable I got the knowledge about RSYNC.
Thanks to Author
HI Vijay,
Thanks for your good words. :)
how to copy live backup like sql,outlook
and block level backup in rsync
@Mehul,
Your question not clear to me..can you explain more briefly….
Thanks, been a great help :)
Great article, found it very useful :)
Thank you very much, these Rsync tutorials really helped me out as I’m new to this stuff. Great blog and easy to understand explanations :) Thank you
Great article, thank you. I have not found a more concise source that still contains Rsync’s core functionality.
As a suggestion, you may want to emphasize that the `-e` option should often be followed by `”ssh -i /path/to/key/”` instead of just plain `ssh`. This is required for accessing an EC2 instance using AWS’s .pem keys, although I’m not sure when just `ssh` is used. Was the `ssh` following the `-e` just for the example?
Such a nice way to describe in easy way……. Is there any documentation or posts for shell scripting.
Yes, at the navigation, you will find a link to Shell Scripting.
“2. Copy/Sync Files and Directory to or From a Server”
This just saved me so much time!
We tried doing it with tar but that didn’t work. cpio is too complicated.
Rsync rocks! Thanks!
Good article. #8 was exactly what i was looking for. And this page was result #5 in Google search. Keep it up. Good job.
If you like the features of ‘rsync’ but are reluctant because of all of the command line options, consider LuckyBackup [see http://sourceforge.net/projects/luckybackup/?source=navbar ].
You get an end-user and a super-user edition of scripts that wrap rsync commands for the most common file backup and restore operations.
Version 0.4.7-2 can be found in the Linux Mint-15 and corresponding Ubuntu repositories.
I feel certain it is available elsewhere.
I don’t want to over right A Server files to B files, if I made changes in B server files
Hi, some one can help me in rsync.
There are two servers, I have written a cron to rsync jpg file from A to B. if I modified some files in B and keep same name, When the cron run it over right files with A server’s files.
I m using
rsync -avz ASeverPath BServerPath
Hi All,
Could you please provide a shell script for the below purpose using Rsync?
– content sync from source to target machine, based on a extensive list of directories and hostnam/ip
– search and replace of the mapped texts from source to target
– replacement should be only in changed/updated files and to a configurable set of file extensions.
Thanks.
Hi All,
Thanks for sharing the examples of rsync. Could you please help me out in writing the script for below purpose using Rsync:
– content sync from source to target machine, based on a extensive list of directories and hostnam/ip
– search and replace of the mapped texts from source to target
– replacement should be only in changed/updated files and to a configurable set of file extensions
Thanks in Advance!!
Nice one, thanks
Very complete tutorial, Thanks!
I am a linux newbie, Please tell me “What is the best way to backup files ?”
please help.
need help on rsync an 2000Mb file . please note on 500mb no problem .. Thanks
Benson,
Should be no problem when rsync 2000Mb file. I always use “screen” if transferring large file size.
https://www.tecmint.com/screen-command-examples-to-manage-linux-terminals/
There is a common confusion in your synching folders. It is one I frequently make and is a gotcha in rysnc. You MUST add the trailing slash, otherwise it will put the folder in your local folder, rather than sync the two:
rsync -avzh /root/rpmpkgs /tmp/backups/
will place the rpmpkgs in your backups folder. If you want to synch two folders that already exist:
rsync -avzh /root/rpmpkgs/ /tmp/backups/rpmpkgs
This for me is a very important little detail!
Thanks for practical good examples on using rsync. One is almost what I need but it includes username and password to remote server. But in my case it is not required.
How the rsync command should look like if I want to use it to periodically check for new radio podcasts on public web server and in case new podcasts are found download it to my local machine. Example: podcasts located at http://213.215.116.181:8001/fm/DATE/FILENAME.mp3 where the DATE usually updates weekly, and FILENAME allways stays the same.
Real file example http://213.215.116.181:8001/fm/2013-12-10/951-Experimental_FM-22-00.mp3
Nice post, I am using rsync to backup my files locally. I am trying to use the find command with rsync so that it can find the latest file in a certain folder remote and sync it to my local folder but it is not working.
Could you update your post to include such a situation.
Thanks.
Hello Tarunika:
Excellent article! Well written. I also liked the practical examples that you presented. Now it’s time for me to start using this powerful and versatile tool. I will also take a look at some of your other articles you posted.
Thanks again.
Andre
Hi Tecmint,
Found this blog today by chance. Lots of information. Kudos to the team.
Coming to this topic, for rsync to work across servers , I believe the private key and public keys should be installed on the source and destination servers respectively. Isnt it so?
If so, could you explain the process of generating keys and adding them to authorized_keys?
Thanks
Chaitu
Yes! you correct, you should generate SSH private and public keys to use rsync to work across servers. For generating SSH keys, you should follow article Create SSH Keys for Passwordless Login.
Great article
Currently I use rsync over 2 servers to backup a whole directory with some 20 sub directories… not all of them are needed/all files needs to be copied…
Could rsync use files as input ? ie a file to exclude specific file types and/or to exclude specific folders as well ?
thanks in advance
Hey Tarunika shrivastava
very useful post…..
I am new to linux , will you please send me some pdf on my email id – [email protected]….
Very good !
is also useful:
rsync with alternative ssh port:
rsync -avh -e ‘ssh -p 2222’ ~/local_directory/ user@servername:remote_diretory/
Interesting article. I would like to add that if you’re syncing data over ssh but ssh is listening on a non-default port you may use the following command:
rsync -Waq -e ‘ssh -p ‘ /path/to/source/ :/path/to/target/
or another nice option, especially if you’re syncing a VPS is the ‘–numeric-ids ‘ flag which will not map uid/gid by actuall user/group, for example:
rsync -Waq -e ‘ssh -p ‘ –numeric-ids /path/to/source/ :/path/to/target/
Thanks.
That was a pretty good introduction and quick overview of using the rsync command.
I do have one suggestion:
Perhaps it would be worthwhile to point out how including or leaving off a trailing slash for directory names/path names affects the behaviour of rsync?
Tarunika…..Thank u so much.!
Nice post here. Highlighted a lot of rsync’s features.
Might want to mention Grsync as a tool for beginners, since it offers a neat way to fiddle with rsync and even allows for generating a command line version of every option selected.
Hi Dumitru,
Thanks, Grsync is a GUI tool for syncing data. I’ll cover this topic in more details in our future articles.
Actually, Grsync is just a GUI wrapper around rsync. It only allows to select options for running rsync with, and nothing more. I mean, without having rsync already installed, Grsync does absolutely nothing in terms of syncing.
A picky comment, I know, but I like to differentiate tools and tool handles.
Hi Tarunika,
Thanks for sharing the examples of rsync. Could you please help me out in writing the script for below purpose using Rsync:
– content sync from source to target machine, based on a extensive list of directories and hostnam/ip
– search and replace of the mapped texts from source to target
– replacement should be only in changed/updated files and to a configurable set of file extensions
Thanks in Advance!!
hii
thanks for the information
can u plz tell about scp also
:)
Hi Hari,
Thanks for your comment, I’ll write an article on SCP (Secure copy) very soon.
I Am working as Asst Manager I.T and looking your Support , Thanks