• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 884
  • Last Modified:

Script to compare tar files

using cygwin on windows and trying to compare two tar files and to print out the difference between them.
0
raaj4354
Asked:
raaj4354
3 Solutions
 
Duncan RoeSoftware DeveloperCommented:
A general method could be to list the contents of each tar file to a separate file then diff the files. This is a quick script to do that
23:40:56$ cat tardiff
#!/bin/sh
fileA=$(tempfile)
fileB=$(tempfile)
tar -tvf $1 | sort >$fileA
tar -tvf $2 | sort >$fileB
diff $fileA $fileB
rm $fileA $fileB

Open in new window

By using sort, the script can meaningfully compare 2 tar files that were not made in the same order. Otherwise it's very basic: invoke as

tardiff file1.tar file2.tar

Modern tar doesn't care if the files are compressed - tar -tf will work on tar, tar.bz2, tar.gz &c. equally well.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Do you just want to compare the filenames that are in the .tar files? That is what the diff above is doing. If you want to compare the files then you'd have to find files with the same name in tar1 and tar2 first and then compare each file. To do this you need to untar both archives as well.
0
 
Duncan RoeSoftware DeveloperCommented:
Actually the above diff has a good chance of finding file changes because of using tar -tvf. The v means you get file size and modification date - quite a good indication of any change although not rigorous.
To only compare file names, use tar -tf
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
raaj4354Author Commented:
I need to compare the file contents of two tar files and list the names of the different files(diff in content) and any new files in either tar files.
0
 
Duncan RoeSoftware DeveloperCommented:
This script diffs the contents
#!/bin/sh
fileA=$(tempfile)
fileB=$(tempfile)
rm $fileA $fileB
home=$PWD
mkdir $fileA $fileB
cd $fileA && tar -xf $1
cd $fileB && tar -xf $2
cd $home
diff -c -r $fileA $fileB
rm -rf $fileA $fileB

Open in new window

Did you mean you want a context diff? Otherwise miss out -c after diff. Unique files will be reported in lines starting Only
0
 
tel2Commented:
Since you just want to "list the names of the different files...", you could change Duncan's:
    diff -c -r $fileA $fileB
line to:
    diff -qr $fileA $fileB
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now