muizelaar has asked for the wisdom of the Perl Monks concerning the following question:

HI I was wondering if anyone knew a way of comparing the contents of two directories and listing missing files only. I have played around with one or two things using the File::DirCompare module with some success but unfortunately I need to install the Perl Module to get it to work which our Server Administrator will not allow :( Please help need to get this sorted quickly :)

Replies are listed 'Best First'.
Re: Comparing Directories
by olus (Curate) on May 01, 2008 at 09:45 UTC
      opendir, readdir and closedir---how did I never know about those? I currently do quite a lot of reading in the contents of directories, and so far I've always done it by endless constructs like
      $dir_list=`ls -1 */*`; @files=split(/\n/,$dir_list);
      D'oh! and thanks! why_bird
      ........
      Those are my principles. If you don't like them I have others.
      -- Groucho Marx
      .......
Re: Comparing Directories
by why_bird (Pilgrim) on May 01, 2008 at 09:58 UTC

    Also, if your SysAdmin won't let you install Perl modules in the usual place (/usr/lib* usually I think), but you can install elsewhere, this works for me (I tried to find the reference on Google, but couldn't):

    • download, and unzip the file for MyModule from CPAN
    • Extract to directory ~/MyDir
    • cd to ~/MyDir/MyModule
    • type perl Makefile.PL PREFIX="~/PerlModules/"
    • type make test; make; make install as usual for a manual install
    • Find the directory in ~/PerlModules in which MyModule.pm resides.
    • Edit your ~/.cshrc (different for different shells) to contain the line 'setenv PERL5LIB
    • ~/PerlModules/Directory_For_MyModule'

    I know this doesn't help much if you're on Windows and the last step will be different for different UNIX shells, but I was stuck on this for a while, and if you Google, you'll likely find more (and better) information than I can give!

    why_bird

    edit: Found it! Here is a reference about how to install without root permissions, which also tells you an alternative to setting the environment variable PERL5LIB. And it talks about what to do on Windows.

    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
Re: Comparing Directories
by alexm (Chaplain) on May 01, 2008 at 14:31 UTC
    You can use diff (if available) and parse its results:
    $ mkdir -p dir1/a/b/c $ mkdir -p dir2/a/b $ echo 1a1 > dir1/a/1 $ echo 2a1 > dir2/a/1 $ touch dir1/a/2 $ touch dir2/a/2 $ touch dir1/a/b/x $ diff -rq dir1 dir2 Files dir1/a/1 and dir2/a/1 differ Only in dir1/a/b: c Only in dir1/a/b: x