linux两个文件夹内容同步,如何使用命令行工具同步两个文件夹?

TuxForLife的答案很好,但我强烈建议您-c在本地同步时使用。您可以辩解说,为远程同步这样做不值得花费时间/网络代价,但是对于本地文件来说完全值得,因为速度是如此之快。

-c, --checksum

This forces the sender to checksum every regular file using a 128-bit MD4

checksum. It does this during the initial file-system scan as it builds

the list of all available files. The receiver then checksums its version

of each file (if it exists and it has the same size as its sender-side

counterpart) in order to decide which files need to be updated: files with

either a changed size or a changed checksum are selected for transfer.

Since this whole-file checksumming of all files on both sides of the con-

nection occurs in addition to the automatic checksum verifications that

occur during a file's transfer, this option can be quite slow.

Note that rsync always verifies that each transferred file was correctly

reconstructed on the receiving side by checking its whole-file checksum,

but that automatic after-the-transfer verification has nothing to do with

this option's before-the-transfer "Does this file need to be updated?"

check.

这显示了具有相同大小和时间戳的方法会使您失败。

设置

$ cd /tmp

$ mkdir -p {A,b}/1/2/{3,4}

$ echo "\___________from A" | \

tee A/1/2/x | tee A/1/2/3/y | tee A/1/2/4/z | \

tr A b | \

tee b/1/2/x | tee b/1/2/3/y | tee b/1/2/4/z | \

tee b/1/2/x0 | tee b/1/2/3/y0 > b/1/2/4/z0

$ find A b -type f | xargs -I% sh -c "echo %; cat %;"

A/1/2/3/y

\___________from A

A/1/2/4/z

\___________from A

A/1/2/x

\___________from A

b/1/2/3/y

\___________from b

b/1/2/3/y0

\___________from b

b/1/2/4/z

\___________from b

b/1/2/4/z0

\___________from b

b/1/2/x

\___________from b

b/1/2/x0

\___________from b

rsync不复制任何内容,因为文件都具有相同的大小和时间戳

$ rsync -avu A/ b

building file list ... done

sent 138 bytes received 20 bytes 316.00 bytes/sec

total size is 57 speedup is 0.36

$ find A b -type f | xargs -I% sh -c "echo %; cat %;"

A/1/2/3/y

\___________from A

A/1/2/4/z

\___________from A

A/1/2/x

\___________from A

b/1/2/3/y

\___________from b

b/1/2/3/y0

\___________from b

b/1/2/4/z

\___________from b

b/1/2/4/z0

\___________from b

b/1/2/x

\___________from b

b/1/2/x0

\___________from b

正确工作的rsync,因为它比较校验和

$ rsync -cavu A/ b

building file list ... done

1/2/x

1/2/3/y

1/2/4/z

sent 381 bytes received 86 bytes 934.00 bytes/sec

total size is 57 speedup is 0.12

$ find A b -type f | xargs -I% sh -c "echo %; cat %;"

A/1/2/3/y

\___________from A

A/1/2/4/z

\___________from A

A/1/2/x

\___________from A

b/1/2/3/y

\___________from A

b/1/2/3/y0

\___________from b

b/1/2/4/z

\___________from A

b/1/2/4/z0

\___________from b

b/1/2/x

\___________from A

b/1/2/x0

\___________from b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值