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

ako*_*csi 117 linux file-copy synchronization files

从 Windows 迁移到 Linux 后,我想找到 Winmerge 的替代软件,或者学习命令行工具来比较和同步 Linux 上的两个文件夹。如果您能告诉我如何在命令行上执行以下任务,我将不胜感激...(我已经研究了 diff 和 rsync,但我仍然需要一些帮助。)

我们有两个文件夹:“/home/user/A”和“/home/user/B”

A文件夹是存放普通文件和文件夹的地方,B文件夹是备份文件夹,作为A文件夹的完整镜像。(B文件夹中没有用户直接保存或修改的内容。)

我的问题是:

  • 如何列出仅存在于文件夹 B 中的文件?(例如,自上次同步以来从文件夹 A 中删除的那些。)

  • 如何将仅存在于文件夹 B 中的文件复制回文件夹 A?

  • 如何列出两个文件夹中都存在但具有不同时间戳或大小的文件?(自上次同步以来已在文件夹 A 中修改的那些。我想避免使用校验和,因为有数以万计的文件,它会使过程太慢。)

  • 如何将文件夹A的精确副本复制到文件夹B中?我的意思是,将文件夹 A 中的所有内容复制到仅存在于文件夹 A 中的文件夹 B 中,并删除仅存在于文件夹 B 中的文件夹 B 中的所有内容,但不涉及两个文件夹中相同的文件。

Tux*_*ife 166

这会将文件夹 A 放入文件夹 B:

rsync -avu --delete "/home/user/A" "/home/user/B"  
Run Code Online (Sandbox Code Playgroud)

如果您希望文件夹 A 和 B内容相同,请将/home/user/A/(带斜杠)作为源。这不是文件夹 A 而是它的所有内容,并将其放入文件夹 B 中。像这样:

rsync -avu --delete "/home/user/A/" "/home/user/B"
Run Code Online (Sandbox Code Playgroud)
  • -a 执行同步保留所有文件系统属性
  • -v 详细运行
  • -u 仅复制修改时间较新的文件(如果时间相等,则复制大小不同)
  • --delete 删除源文件夹中不存在的目标文件夹中的文件

手册页:https : //download.samba.org/pub/rsync/rsync.html

  • **rsync**:运行 rsync 应用程序,**-a**:进行同步,保留所有文件系统属性,**-v**:详细运行,**-z**:在同步期间压缩数据(传输压缩模式下的数据),**--delete**:删除源文件夹中不存在的目标文件夹中的文件,**/home/user/A**:源文件夹,**/home/user/ B**:目标文件夹 (11认同)
  • Rsync 主要用于在不同计算机之间复制文件,正如这里所解释的,它也可以用于同步目录。所以 -z 选项很有趣,可以减少网络流量,从而提高 2 台计算机之间 rsync 的性能:(从磁盘读取数据 -> 压缩)===网络===>(解压缩->写入磁盘)使用 - z 在同一台主机上同步 2 个目录有点傻,而且会浪费 CPU 周期(从磁盘读取数据 -> 压缩 -> 解压缩 -> 写入磁盘) (9认同)
  • 响应 Luke 关于尾随斜杠的评论的重要说明 - 来自联机帮助页:“源上的尾随斜杠会更改此行为,以避免在目标处创建额外的目录级别。您可以将源上的尾随 / 视为表示“复制”该目录的内容”与“按名称复制目录”相反,但在这两种情况下,包含目录的属性都会传输到目标上的包含目录。- 最重要的是,如果您不想在目标上创建子目录,请在源中添加尾部斜杠 (3认同)
  • 我已经尝试过该命令,但它创建了一个子目录 `/home/user/B/A` 而不是将 A 的内容覆盖到 B 的内容。你能帮我看看吗? (2认同)

axo*_*otl 16

您可以使用unison宾大的 Benjamin Pierce 开发的工具。

让我们假设您有两个目录,

/home/user/Documents/dirA//home/user/Documents/dirB/

要同步这两者,您可以使用:

~$unison -ui text /home/user/Documents/dirA/ /home/user/Documents/dirB/

在输出中,unison将显示您要求同步的两个目录中不同的每个目录和文件。它将建议在初始运行时附加同步(在两个位置复制丢失的文件),然后在您的机器上创建和维护一个同步树,并在随后的运行中实现真正的同步(即,如果您从 中删除文件.../dirA,它也将被删除.../dirB。您还可以比较每个更改,并可选择在两个目录之间进行正向反向同步。

或者,要启动图形界面,只需-ui text从命令中删除该选项,尽管我发现使用起来cli更简单快捷。

更多相关信息:Unison 用户文档中的 Unison 教程


Bru*_*sky 8

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.
Run Code Online (Sandbox Code Playgroud)

这表明拥有相同的大小和时间戳可能会让您失望。

设置

$ 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
Run Code Online (Sandbox Code Playgroud)

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    
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


Hon*_*Abe 5

这是我用于备份个人文件的方法,我不关心 涵盖的所有内容-a,并且希望打印更多有用的信息。

rsync -rtu --delete --info=del,name,stats2 "/home/<user>/<src>/" "/run/media/<user>/<drive>/<dst>"
Run Code Online (Sandbox Code Playgroud)

rsync 手册页

-r, --recursive
这告诉 rsync 递归复制目录。

-t, --times
这告诉 rsync 与文件一起传输修改时间并在远程系统上更新它们。

-u, --update
这会强制 rsync 跳过目标上存在的任何文件,并且修改时间比源文件新。(如果现有目标文件的修改时间与源文件的修改时间相同,则如果大小不同,它将被更新。)

--delete
这告诉 rsync 从接收方删除无关文件(不在发送方的文件),但仅用于正在同步的目录。

--info=FLAGS
此选项可让您对想要查看的信息输出进行细粒度控制。

rsync --info=help

DEL        Mention deletions on the receiving side  
NAME       Mention 1) updated file/dir names, 2) unchanged names  
STATS      Mention statistics at end of run (levels 1-3)
Run Code Online (Sandbox Code Playgroud)

虽然不太明确,但这似乎是等效的且更短:

DEL        Mention deletions on the receiving side  
NAME       Mention 1) updated file/dir names, 2) unchanged names  
STATS      Mention statistics at end of run (levels 1-3)
Run Code Online (Sandbox Code Playgroud)

-v, --verbose
单个 -v 将为您提供有关正在传输的文件的信息以及最后的简短摘要 [stats1]。