9

rsync command is used to sync a folder data to another folder. I want to know if this command overwrites the entire file and sync or copies only the new file or changed file. Does it prompt if similar name file is found in destination folder??

Specifically, I'm using rsync -rvh /usr/local/aryan/ /usr/local/aryan and it overwrote all the files from local machine to the remote machine. I copied 10 files manually to remote location and then used above rsync command to sync 20 files including first 10 files as well. After rsync -rvh command, I could see all the files in the remote location had same time. It means all the files got overwritten.

2
  • 1
    You should specify exactly what "this command" is: Rsync has many options controlling the behavior.
    – U. Windl
    Jan 18, 2021 at 10:48
  • I found the actual command the OP used, and have added it to the question. The command makes no sense, though. Jan 18, 2021 at 11:29

3 Answers 3

7

rsync will generally create a new version of a file alongside the destination, and then switch it across to the destination name at the last moment, when it is complete. The new version can be created either using deltas, i.e. changes between the source and destination, or as a full copy. (See my answer for Smarter filetransfers than rsync? for some clarity over this difference.)

The --partial flag tells rsync to keep and use any partially transmitted file as the target if the connection is interrupted for any reason. (Otherwise, a partially transmitted file is discarded.) The advantage of this flag is that time and bandwidth spent transferring a file is not wasted when the rsync is interrupted and retried. The disadvantage of this flag is that a file 90% transferred before interruption can end up being replaced with the same file only 10% transferred before interruption.

The --inplace flag tells rsync to build the destination file directly in place. This is often less efficient in time but avoids having two full copies of the destination file on the remote filesystem simultaneously. In the general case this flag should not be used (and indeed it conflicts with some other options such as --sparse).

3

Uh, it really depends on the flags passed to rsync, but by default rsync mirrors entire file(s) from the source to the destination, though you can customize what happens to a degree with --update or --existing or --ignore-existing or also various exclude complications (--exclude, --delete, --delete-excluded) among other options (--prune-empty-dirs, etc).

There is (thank goodness!) no prompting (except for passwords), and no support for "similar name" that I know of (what use case would require that?).

1
  • m using rsync -rvh /usr/local/aryan/ /usr/local/aryan and it overwrote all the files from local machine to the remote machine. I copied 10 files manually to remote location and then used above rsync command to sync 20 files including first 10 files as well. After rsync -rvh command, I could see all the files in the remote location had same time. It means all the files got overwritten.
    – Aryan
    Sep 9, 2015 at 21:10
1

From the man page:

It is famous for its delta-transfer algorithm, which reduces the amount 
of data sent over the network by sending only the differences between 
the source files and the existing files in the destination.

You can see this in action with a test:

$ mkdir foo bar
$ echo "a" > foo/a
$ rsync -av --stats foo/ bar/
sending incremental file list
./
a

Number of files: 2 (reg: 1, dir: 1)
Number of created files: 1 (reg: 1)
Number of deleted files: 0
Number of regular files transferred: 1
--> Total file size: 2 bytes
--> Total transferred file size: 2 bytes
...blah blah blah...

This establishes that an initial rsync sends 2 bytes of data from source to target dirs.

Now add a new file and rsync it:

$ echo "b" > foo/b
sending incremental file list
./
b

Number of files: 3 (reg: 2, dir: 1)
Number of created files: 1 (reg: 1)
Number of deleted files: 0
Number of regular files transferred: 1
--> Total file size: 4 bytes
--> Total transferred file size: 2 bytes
...blah blah blah...

Here, rsync correctly sees that all files total 4 bytes, but that only 2 need to be sent. In other words, it does not overwrite the existing file.

1
  • 1
    Your conclusion is correct but your methodology is flawed. The delta algorithm is not used when copying to a local filesystem. (See the last sentence for the --whole-file option: « This [the whole-file option] is the default when both the source and destination are specified as local paths » and this answer that explains why.) Sep 9, 2015 at 22:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .