In the terminal, combining several folders into one

I have a backup directory created by WDBackup (Western Digital External HD Archive) that contains the directory for each day that it backed up and the incremental content of just what was backed up.

So the hierarchy looks like this:

20100101
  My Documents
    Letter1.doc
  My Music
    Best Songs Every
      First Songs.mp3
    My song.mp3 # modified 20100101
20100102
  My Documents
    Important Docs
      Taxes.doc
  My Music
    My Song.mp3 # modified 20100102

...etc...

      

Just changed, the backup and the first backup ever created contains all the files selected for backup.

What I'm trying to do now is a step-by-step copy, keeping the folder structure from oldest to newest, each of these dated folders into a "merged" folder so that it overlaps old content and preserves new material.

As an example, if only using these two example folders, the final merged folder looks like this:

Merged
  My Documents
    Important Docs
      Taxes.doc
    Letter1.doc
  My Music
    Best Songs Every
      First Songs.mp3
    My Song.mp3 # modified 20100102

      

Hope this makes sense.

Thanks,

Josh

+2


a source to share


2 answers


You can use rsync in a bash for loop like:

$ for d in 2010* ; do rsync -av ./$d/ ./Merged/ ; done

      



Note that before running this for real you can just be careful and check that it actually does what you want - for that you can use the rsync flag -n

to dry run:

$ for d in 2010* ; do rsync -avn ./$d/ ./Merged/ ; done

      

+5


a source


If the directory names are indeed in the form YYYYMMDD, you can simply sort them. So it's just a matter of traversing them one by one, starting with the older one and copying everything to the destination, overwriting the previous things. Quite inefficient, but works.

cd $my_disk_full_of_backups
for x in (ls | sort); do cp -Rf $x/* /my_destination/; done

      



Ugly, it should also be portable enough for almost every unix system that has bash.

See bash (1), ls (1) and collation (1) for details.

+1


a source







All Articles