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

Hello monks!
I have 2 big directories, say BIG_1 and BIG_2 that have 100 subdirectories each. The subdirectories have the same name in both BIG directories, that is BIG_1 contains SMALL_1, SMALL2... SMALL100 and BIG_2 also contains SMALL_1, SMALL2...SMALL100.
What I want to do is to merge the contents of each subdirectory of BIG_1 with BIG_2. That is, if for example SMALL_1 of BIG_1 has doc1, doc2 and doc3 and SMALL_1 of BIG_2 has doc4 and doc5, I want all contents of SMALL_1 of BIG_2 to be moved to SMALL_1 of BIG_1, so that SMALL_1 of BIG1 will finally have doc1, doc2, doc3, doc4 and doc5 and SMALL_1 of BIG_2 will be empty. The same applies to all subdirectories.

Replies are listed 'Best First'.
Re: Move files between directories
by Tanktalus (Canon) on Aug 31, 2009 at 22:47 UTC

    That's a bit difficult to read. But what you basically seem to need is: glob, chdir, and File::Copy::move. So, what have you tried?

Re: Move files between directories
by graff (Chancellor) on Sep 01, 2009 at 00:16 UTC
    What if you have BIG_1/SMALL_1/doc1 and also BIG_2/SMALL_1/doc1 (that is, matching file name in both paths), but those two files happen to be different? Would the file under BIG_1 always be the one to keep, or would you replace it with the version under BIG_2, depending on size or age or whatever?

    Anyway, you might just want to create two lists, one for all the data files under BIG_1, and another for all the data files under BIG_2 (File::Find or one of its variants could help with that, or the gnu "find" command-line utility). Then, the files to move are the ones in the BIG_2 list that are not in the BIG_1 list.

    Writing your script to store the lists in memory as separate hashes (with "SMALL_n/filename" as the hash keys) would make it quick and easy to tell which files need to be moved. Or, if the lists are sorted, you could just use the gnu "diff" and "grep" to compare the lists and get the files that appear only in BIG_2.

      Guys thanks, I just copied all subdirectories from BIG_1 into BIG_2 and their files were merged... :)
      Thanks anyway...
Re: Move files between directories
by Sewi (Friar) on Sep 01, 2009 at 07:10 UTC
    Not a perl solution, but may be good for you if you're on Linux:

    find BIG_1 -type f -exec touch \{\} \;

    cp -au BIG_2/* BIG_1/

    This will update the modification time of all files in BIG_1 and than copy all files from BIG_2 which don't exist in BIG_1 to there (-u means update: copy only files which have no newer file of the same name at the target).