Tar 与原始文件夹递归比较

Tar recursive compare with original folder

我有一个用 tar cvzf tartest.tgz tester/* 制作的 .tgz 文件,如果我用 tar --list -f tartest.tgz 文件列出 tar,我有以下结构

 tester/2017-08-02_131404.png
 tester/cfg.pdf
 tester/tests/
 tester/tests/1.png
 tester/tests/2.png
 tester/tests/3.png
 tester/tests/4.png
 tester/tests/5.png

如果我使用 tar -df tartest.tgz tester/* 将 tar 与原始文件夹进行比较,一切正常,没有问题,没有错误

如果我在 tester 文件夹中添加文件 20171006_183137.png 并重试,我会如预期的那样收到错误:

 tar: tester/20171006_183137.png: Not found in archive
 tar: Exiting with failure status due to previous errors

如果我在 tester/tests 文件夹中添加文件 20171006_183137.png,然后重试,我没有错误和空白输出。

如果我在上次测试中添加 -v 选项,我只会得到 tar.

中的文件列表

有没有办法递归比较 tar 与原始文件夹和子文件夹?

根据 this sitetar 按预期运行。

You should note again that while --compare (-d) does cause tar to report back on files in the archive that do not exist in the file system, tar will ignore files in the active file system that do not exist in the archive.

您收到的关于 tar -df tartest.tgz tester/* 的错误确实是 错误 (!),而不是像 »archive and directory differ[=36] 这样的消息=]«。 tar 不知道如何处理不在存档中的文件。

如果您还想进行相反的比较,可以使用this answer中描述的方法(挂载或解压存档并使用diff -r对原始目录)。

如果您只对文件的存在而不是内容、访问日期等感兴趣,您可以列出存档和原始目录中的文件名,并将它们与 diff 进行比较:

diff <(tar -tf tartest.tgz | sort) <(find tester/ | sort)

该命令仅在文件名中没有换行符时才有效。
使用 diff -ycomm 命令以获得更易读的输出。