Tar recursive compare with original folder

Viewed 904

I have a .tgz file made with tar cvzf tartest.tgz tester/* and if I list the tar with tar --list -f tartest.tgz file, I have the following structure

 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

If I compare the tar with the original folder by using tar -df tartest.tgz tester/*, everything ok, no problems, no errors

If I add the file 20171006_183137.png in the tester folder, and retry, I get an error, as expected:

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

If I add the file 20171006_183137.png in the tester/tests folder, and retry, I get no error and blank output.

If I add -v option during last test, I just get the list of the files in the tar.

Is there a way to recursive compare tar with original folder and subfolders?

1 Answers

According to this site, tar behaves as intended.

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.

The error you are getting for tar -df tartest.tgz tester/* is indeed an error (!) not a message like »archive and directory differ«. tar does not know how to treat files that are not in the archive.

If you also want to compare the other way around, you could use the method described in this answer (mount or unpack the archive and use diff -r against the original directory).

If you are only interested in a file's existence and not content, access dates, and so on, you could list the file names from the archive and from the original directory and compare them with diff:

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

The command works only if there are no file names with linebreaks in them.
Use diff -y or the comm command for a more readable output.

Related