Re: Comparing two directories

From: Vladimir Lemberg (lemba_at_sbcglobal.net)
Date: 10/28/04


To: "Vladimir Lemberg" <lemba@sbcglobal.net>, <beginners@perl.org>
Date: Wed, 27 Oct 2004 17:16:58 -0700

Actually, this is my script:

foreach $filename(readdir DIR1) {
 next if $filename =~/^\./;
  while (readdir DIR2){
  print "$filename\n" if (-e $filename);
  last;
  }
}

----- Original Message -----
From: "Vladimir Lemberg" <lemba@sbcglobal.net>
To: <beginners@perl.org>
Sent: Wednesday, October 27, 2004 5:14 PM
Subject: Comparing two directories

Hi,

 

Could you help me to find what's wrong in my script?

 

I need to compare two directories and if file exists in both - print it.

 

die "Usage: 2 arguments must be passed i.e. file1 file2\n" if @ARGV != 2;
opendir DIR1, $ARGV[0] or die "couldn't open $ARGV[0] for reading.\n";
opendir DIR2, $ARGV[1] or die "couldn't open $ARGV[1] for reading.\n";

foreach $filename(readdir DIR1) {
   next if $filename =~/^\./;
     while (readdir DIR2){
       if (-e $filename){
       print "$filename\n"}
     }
 }
closedir(DIR1);
closedir(DIR2);

 

Looks like I'm checking "if exists" in first directory but not in second.

 

-Vladimir



Relevant Pages

  • Re: [SLE] slow reiserfs?
    ... >> script is running diff on each pair. ... and that you're not comparing all the possible combinations ... the shell does a lot of ... Keep in mind, too, that when you use Perl to invoke external commands such ...
    (SuSE)
  • Re: [fw-wiz] Application Intelligent vs ALG
    ... inspect it just that you had to write an inspect script and really know ... Does it provide any better coverage than a proxy. ... guess as everything it depends what you are comparing it against. ... >CONFIDENTIALITY CAUTION: ...
    (Firewall-Wizards)
  • Re: how can I make this script shorter?
    ... Good idea about hashing part of the file before comparing entire files. ... It will make the script longer but the speed increase will most likely ... > lots of comparisons if you did match based on some hash. ...
    (comp.lang.python)
  • Re: Southeast Asian script
    ... As the antiquarian could not tell us which country it ... I tried to learn about its origin by comparing the script ...
    (sci.lang)
  • Re: Diff two files
    ... Hello.It's a simple comparing from the first file to the second file. ... Given two example files here and run the script to get the results: ... $ cat 1.txt ...
    (perl.beginners)