Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Location
    india
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I compare file list from a tar archive and directory?

    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.

  • #2
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what I ment to say is, post your code so people can look at it dude.

    TransitCop

  • #4
    New to the CF scene
    Join Date
    Jul 2009
    Location
    india
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

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


    Code:
    #! /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;
    }

  • #5
    New Coder
    Join Date
    Aug 2009
    Location
    in front of the keyboard
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by vinodkaushik View Post
    Hi,

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


    Code:
    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 :
    Code:
    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 :
    Code:
    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 :
    Code:
    #! /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.
    Last edited by Jolle; 08-21-2009 at 07:26 PM. Reason: added test program

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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.

    Code:
    #!/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};
        }
    }


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •