Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to compare tar files

Posted on 2012-09-16
6
Medium Priority
?
784 Views
Last Modified: 2013-11-15
using cygwin on windows and trying to compare two tar files and to print out the difference between them.
0
Comment
Question by:raaj4354
6 Comments
 
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 1600 total points
ID: 38405472
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38405653
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
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 38407411
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:raaj4354
ID: 38407527
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
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 1600 total points
ID: 38407536
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
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 400 total points
ID: 38407557
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question