I found the "rsync --link-dest" behaves differently depending on how I created the comparison directory. If the comparison directory was created by a "cp" command, the "rsync --link-desk" does the hard-link properly. However, if the content of the comparison directory was created manually, even the content is the same, the same rsync command will not do hard-link but copy. Here is an example:
$ mkdir /tmp/src $ echo "test" > /tmp/src/file $ cp -a /tmp/src /tmp/dest1 $ rsync -ai --link-dest=/tmp/dest1 /tmp/src/ /tmp/dest2 $ ls -l /tmp/dest2/ total 4 -rw-rw-r-- 2 user user 5 Apr 7 23:24 file
In the example above, "rsync --link-dest" worked as expected. If we create the content of comparison directory "/tmp/dest1" manually, the same command does not do hard-link:
$ rm -rf /tmp/src /tmp/dest1 /tmp/dest2 $ mkdir /tmp/src $ echo "test" > /tmp/src/file $ cat /tmp/src/file > /tmp/dest1/file $ md5sum /tmp/src/file /tmp/dest1/file d8e8fca2dc0f896fd7cb4cb0031ba249 /tmp/src/file d8e8fca2dc0f896fd7cb4cb0031ba249 /tmp/dest1/file $ rsync -ai --link-dest=/tmp/dest1 /tmp/src/ /tmp/dest2 cd..t...... ./ >f..t...... file $ ls -l /tmp/dest2 total 4 -rw-rw-r-- 1 user user 5 Apr 7 23:31 file
This time the "rsync --link-dest" command copied the file in the source directory to the destination directory instead of hard-linking it, even the content and the directory layout between the source and comparison directories are the same.
So what is different between "cp" and manually create the content to the "rsync --link-dest" command? I am using rsync 3.1.2 and the same behaviour can be observed on both Ubuntu 18.10 and MacOS 10.14.3. Thanks.