Maybe you want the directories that you synchronize to be exact copies of each other or maybe you just don't. Let's dig a little more deeply into the rsync command and see if we can't find just the right mix of options for what you want to do.
The rsync command can replicate collections of files from one place to another in every possible detail or it can allow you to control exactly how that replication flows -- what it replicates and what it doesn't.
In its simplest form, the rsync command will copy files from the file source to the file destination. It will not remove files on the destination side that aren't on the source and it won't recreate all of the metadata (e.g., ownership and group details) unless your rsync command includes just the right set of options. So let's follow along behind a series of rsync commands to see just what they do and don't do in response to our synchronization requests.
First, if you specify the name of a directory as a source, rsync is going to create (or update) a directory by that name on the source location as you can see from this example. Here, we're working with two folders on the same system. When we start, both of the orig and copy directories exist, but have only some files in common.
$ ls orig a b c d e f $ ls copy a c e g
When we run the rsync command with the -v (verbose) option, we can see that the command is copying all of the files from one directory to the other.
$ rsync -av orig/* copy sending incremental file list a b c d e f sent 311 bytes received 126 bytes 874.00 bytes/sec total size is 0 speedup is 0.00
Since almost no data was transferred with these small files, the command moves along, but no speedup is observed. Look at the destination directory afterwards and we can see that it now contains all of the files in the orig folder. It also retains the one file on the destination that was there before the rsync operation (and doesn't exist on the source.
$ ls copy a b c d e f g
Staying in sync
In the next example, we are working between two systems and replicating a directory called "archive". The first operation copies everything, creating the archive directory on the remote system. We use just ~unidweeb as the destination on the remote system (home of the user called "unixdweeb").
orig$ rsync -av archive remhost:~unixdweeb building file list ... done archive/ archive/try1 archive/tryme archive/tryme1 archive/tryme2 sent 1486 bytes received 114 bytes 1066.67 bytes/sec total size is 1170 speedup is 0.73
Sign up for CIO Asia eNewsletters.