为什么 rsync 在这里忽略较新的目录?

为什么 rsync 在这里忽略较新的目录?

我有两个包含很多子目录的目录。我想比较目录并找到第二个目录中缺少的子目录以及第一个目录中较新的子目录。为了找到一种方法,我创建了测试目录。第一个看起来像这样:

name@pc:~/test> ls -l dir1/
insgesamt 28
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir1
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir2
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir3
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir4
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir5
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir6
drwxrws--- 2 name 1111111 4096 12. Jan 11:35 testdir7

第二个看起来像这样:

name@pc:~/test> ls -l dir2/
insgesamt 20
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir1
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir2
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir3
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir4
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir7

正如您所看到的,第二个目录中缺少 testdir5 和 testdir6,并且 testdir7 不是最新的(它比第一个目录中的旧)。这 3 个目录(testdir5、testdir6 和 testdir7)是所需的输出。子目录在两个目录中也不包含相同的文件/内容,因为它们在转换过程之前和之后。所以我认为在 rsync 中使用 -r 选项是不适用的。以下命令应该给出目录差异(丢失或过时):

name@pc:~/test> rsync --update -dvn dir1/ dir2/
building file list ... done
testdir5/
testdir6/

sent 164 bytes  received 18 bytes  364.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

目录的 -d 选项。 -v 用于列表输出。 -n 因为我只对差异感兴趣。无论如何,--update 选项没有任何区别。为什么我在这里没有得到 testdir7/ ?显然,目录修改日期显示它较旧。我认为这与 rsync 决定某些内容是否不同的方式有关,并且事实上这些只是目录,但据我所知,没有“仅使用修改时间”选项。我应该在这里做什么不同的事情?

编辑:测试一些答案后的结果

name@pc:~/test> rsync -dnvt dir1/ dir2/
building file list ... done
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir8/

sent 206 bytes  received 43 bytes  498.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

name@pc:~/test> rsync -dnvt --ignore-existing dir1/ dir2/
building file list ... done
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir8/

sent 206 bytes  received 43 bytes  498.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

name@pc:~/test> rsync -dnvt --update dir1/ dir2/
building file list ... done
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir8/

sent 206 bytes  received 43 bytes  498.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

name@pc:~/test> rsync -anv --ignore-existing dir1/ dir2
sending incremental file list
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir7/testfile1.txt
testdir8/

sent 300 bytes  received 54 bytes  708.00 bytes/sec
total size is 9  speedup is 0.03 (DRY RUN)

答案1

您没有要求 rsync 遍历目录,因此它只是同步目录本身,而不是目录内的文件。如果您希望 rsync 考虑目录内的文件,您需要传递选项-r-a,或 在命令行上列出文件。

您尚未要求 rsync 复制任何元数据。默认情况下,rsync 仅复制内容。对于目录来说,没有内容,只有目录本身的存在:目录的内容(其中的文件)被视为可以独立复制(并受过滤器影响)的单独对象。由于testdir7目标树中已存在,因此无需对其执行任何操作。如果要复制目录上的时间戳,则需要传递选项-t-a

请注意,复制目录上的时间戳而不复制其中的文件很少有用。如果您在目录中创建(或删除等)文件,则会更新目录的修改时间。另一方面,修改现有文件或更改目录不会更新目录的修改时间。如果更新dir2/testdir7和的修改时间然后将文件从dir1/testdir7复制到dir2/testdir7, 的修改时间dir2/testdir7将是文件复制的时间。 Rsync 不会根据目录的时间戳来决定复制目录的内容,因为这不提供有关目录内容状态的任何信息。

答案2

您还需要指定-t

--times, -t              preserve modification times

所以这应该有效:

rsync -dvnt dir1/ dir2/

相关内容