Script to compare two directory structures

Discussion in 'Perl Misc' started by Generic Usenet Account, May 11, 2007.

  1. We had a need to compare two directory structures to see if they are
    identical (meaning if they have the same structure, same contents and
    same versions of files). I wrote a shell script for this purpose
    (posted to the comp.sources.d newsgroup). It works, but given my
    scant knowledge of scripting, it is rather crude. I am looking for
    something more professional and robust, perhaps using perl. Any help
    would be appreciated.

    Thanks,
    Bhta
    Generic Usenet Account, May 11, 2007
    #1
    1. Advertising

  2. Generic Usenet Account

    Ed Morton Guest

    Generic Usenet Account wrote:
    > We had a need to compare two directory structures to see if they are
    > identical (meaning if they have the same structure, same contents and
    > same versions of files). I wrote a shell script for this purpose
    > (posted to the comp.sources.d newsgroup). It works, but given my
    > scant knowledge of scripting, it is rather crude. I am looking for
    > something more professional and robust, perhaps using perl. Any help
    > would be appreciated.
    >
    > Thanks,
    > Bhta
    >


    diff -r dir1 dir2

    If that doesn't work for you, explain why.

    Ed.
    Ed Morton, May 11, 2007
    #2
    1. Advertising

  3. On May 11, 10:14 am, Ed Morton <> wrote:

    >
    > diff -r dir1 dir2
    >
    > If that doesn't work for you, explain why.
    >
    > Ed.


    Sheepishly I must admit that you are absolutely right. Thanks for
    pointing this out ----- you made me realize how I wasted a couple of
    hours of precious time on a wild goose chase.
    Generic Usenet Account, May 11, 2007
    #3
  4. On May 11, 10:14 am, Ed Morton <> wrote:
    > Generic Usenet Account wrote:
    > > We had a need tocomparetwodirectorystructuresto see if they are
    > > identical (meaning if they have the same structure, same contents and
    > > same versions of files). I wrote a shellscriptfor this purpose
    > > (posted to the comp.sources.d newsgroup). It works, but given my
    > > scant knowledge of scripting, it is rather crude. I am looking for
    > > something more professional and robust, perhaps using perl. Any help
    > > would be appreciated.

    >
    > > Thanks,
    > > Bhta

    >
    > diff -r dir1 dir2
    >
    > If that doesn't work for you, explain why.
    >
    > Ed.


    There is at least one situation where my extremely crude script works,
    but diff -r dir1 dir2 does not. For example, the script will work
    even if the two directory structures are not visible on the same
    system e.g. one directory is on the build server while another
    directory is on the test server, and there is no cross mounting
    between the two.

    Bhta
    Generic Usenet Account, May 16, 2007
    #4
  5. In article <>,
    Ed Morton <> wrote:
    ....
    >diff -r dir1 dir2
    >
    >If that doesn't work for you, explain why.
    >
    > Ed.


    From what I can tell, "diff -r" actually compares (performs the 'diff'
    functionality on) every file that it encounters. That's not strictly
    speaking the same as that which most people intuitively assume when they
    think of comparing directory structures.

    What I am getting at is that what "most people intuitively assume when
    they think of comparing directory structures" is comparing things at the
    time-stamp level, not at the file contents level. The implications of
    this are:
    1) That comparing byte for byte is time-intensive (and usually
    unnecessary)
    2) That, especially on non-Unix systems, performing the 'diff'
    functionality on binary files is not exactly a well-defined
    operation.

    Therefore, it would still be nice to have a Unix-based util that is
    more akin to the sorts of things that many of us are familiar with on,
    e.g., the MS Windows platform.
    Kenny McCormack, May 20, 2007
    #5
  6. Jim Gibson wrote:

    > I am not familiar with utilities to compare directory structures on the
    > MS Windows platform, but were I to write a program to compare directory
    > structures (not file contents), I would use the File::Find module to
    > recursively get file names in each directory tree, the stat function to
    > get the modification times, and hashes to save and compare the
    > resulting data.


    As for the hashes (as in checksums), I'd use the Digest::MD5 module:

    use File::Find to build a (Perl-)hash mapping pathnames to
    node-information, constructing the latter from stat() and md5_hex().
    If the number of entries is huge and memory is tight, you could write
    this information for both trees to a file and then compare the contents
    of these files.

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, May 22, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. david
    Replies:
    2
    Views:
    615
    Dennis Lee Bieber
    Nov 9, 2005
  2. yys2000
    Replies:
    0
    Views:
    288
    yys2000
    Nov 9, 2005
  3. GenxLogic
    Replies:
    3
    Views:
    1,242
    andrewmcdonagh
    Dec 6, 2006
  4. Alfonso Morra
    Replies:
    11
    Views:
    702
    Emmanuel Delahaye
    Sep 24, 2005
  5. Sara
    Replies:
    4
    Views:
    497
Loading...

Share This Page