Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tar recursive compare with original folder

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?

like image 824
Kalizi Avatar asked Nov 07 '17 15:11

Kalizi


People also ask

How can I tell the difference between two tar files?

If you can afford to extract just one tar file, you can use the --diff option of tar to look for differences with the contents of other tar file. One more crude trick if you are fine with just a comparison of the filenames and their sizes. Remember, this does not guarantee that the other files are same!

Does tar keep original file?

The * is what tells tar to include all files and local directories recursively. The tar command will never move or delete any of the original directories and files you feed it – it only makes archived copies. You should also note that using a dot (.)

How do I extract a tar file to a specific location?

The other method to extract a “tar” file to some specific folder is using a Linux terminal shell. We need to open the Linux terminal shell from its applications using the “Ctrl+Alt+T” shortcut to use this method. You can also use the activity menu from the taskbar of the Ubuntu 20.04 system to open the shell terminal.

Does tar keep directory structure?

tar stands for “tape archive”. tar, by default, keeps the directory structure of archived files. However, there might be cases when we want to archive files without keeping the directory structure.


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.

like image 116
Socowi Avatar answered Oct 08 '22 11:10

Socowi