设置

设置

从 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 中的所有内容,但不触及两个文件夹中相同的文件。

答案1

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

rsync -avu --delete "/home/user/A" "/home/user/B"

如果你想要文件夹A和B的内容为了保持相同,请将/home/user/A/(带斜杠)作为源。这不是获取文件夹 A,而是获取其所有内容并将其放入文件夹 B。如下所示:

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

联机帮助页:https://download.samba.org/pub/rsync/rsync.html

答案2

您可以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 教程

答案3

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

答案4

你可能会看看Fitus/Zaloha.sh。它是一个以 bash shell 脚本实现的同步器,仅使用标准 Unix 命令。它很容易使用:

$ Zaloha.sh --sourceDir="test_source" --backupDir="test_backup"

相关内容