...

View Full Version : How can I compare file list from a tar archive and directory?



vinodkaushik
08-19-2009, 02:48 PM
I am still learning Perl. Can anyone please suggest me the Perl code to compare files from .tar.gz and a directory path.

Let's say I have tar.gz backup of following directory path which I have taken few days back.

a/file1
a/file2
a/file3
a/b/file4
a/b/file5
a/c/file5
a/b/d/file and so on..

Now I want to compare files and directories under this path with the tar.gz backup file and delete the files in directory which are not available in tar.gz.

Please suggest Perl code to do that.

hackYourBrain
08-19-2009, 11:55 PM
Im dont feel like coding this for you right now.. but I dont get what you dont understand?? the syntax?

use shell escapes to pull the output of the ls command in to a couple variables, one for the tar and one for the dir you want to compare.. run the regex.. rm what you dont want.

deadSpace

hackYourBrain
08-20-2009, 01:06 AM
what I ment to say is, post your code so people can look at it dude.

TransitCop

vinodkaushik
08-20-2009, 08:17 AM
Hi,

I have written below code to compare the files but it is showing all the files in an array2



#! /usr/bin/perl -w

use strict;
use warnings;
use File::Find;
use Cwd;

my $svnpath = "/home/backup";
my $tarfile = "20090818.tar.gz";

chdir "$svnpath";


my @files;
find(\&d, ".");


sub d {
-f and -r and push @files, $File::Find::name;
}

sub getTar {
use Archive::Tar qw();
my @lists = Archive::Tar->list_archive($tarfile);
return @lists;
}

my @array1 = &getTar;
my @array2 = @files;

for my $i (@array2) {
print "$i\n" if grep {$i ne $_} @array1;
}

Jolle
08-21-2009, 08:17 PM
Hi,

I have written below code to compare the files but it is showing all the files in an array2




for my $i (@array2) {
print "$i\n" if grep {$i ne $_} @array1;
}

Off course. grep {$i ne $_} @array1 always returns a list of files that are not equal to $i, so is evaluated true and thus $i prints.

What you want to do is this :

my @missing = grep { my $x = $_; not grep { $x eq $_ } @array1 } @array2;
@missing contains everything from @array2 that is not in @array1.

Or if you want to do it with a loop :

for my $i (@array2) {
print "$i\n" if not grep {$i eq $_} @array1;
}

EDIT :
If you want to see what grep actually returns in your script, execute :


#! /usr/bin/perl -w

my @array1 = ("file1","file2","file3","file6");
my @array2 = ("file4","file3","file1","file5");

for my $i (@array2) {
print grep {$i ne $_} @array1;
print "\n";
}
You realize immediately why it doesn't work.

FishMonger
08-22-2009, 02:47 AM
Using a hash would be more efficient than grepping over the entire @array1 for each for each item in @array2.

I only partially tested this, so it will probably need a slight tweaking.


#!/usr/bin/perl

use strict;
use warnings;
use Archive::Tar;
use File::Find;

my $svnpath = "/home/backup";
my $tarfile = "20090818.tar.gz";

my %list = map { $_, 1 } Archive::Tar->list_archive($tarfile);

find(\&d, $svnpath);


sub d {
if (-f and -r) {
(my $file = $File::Find::name) =~ s/^$svnpath//;
print "$file\n" if not exists $list{$file};
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum