[CentOS] Re: Opposite of cp -u

Mon Aug 6 23:27:20 UTC 2007
Robert Nichols <rnicholsNOSPAM at comcast.net>

Robert Moskowitz wrote:
> I had at one point copied a large number of files between drives and did 
> not use the -p and thus the timestamps were all set to the date of the 
> copy.
> 
> I did not catch this, and deleted the source.  So I 'lived' with it and 
> have since changed many files.
> 
> Well, yesterday I found a good backup of many of those files and I want 
> to restore them to their proper dates.
> 
> cp -p -u is exactly the opposite of what I want.  I want to copy only if 
> the source files have an earlier date than the destination files.

I don't think that's what you want to do.  The files from the backup will
also have an earlier date than the files you subsequently changed.  I
believe what you want is more like this:

     cd /backup/directory
     for F in *
     do  cmp "$F" "/new/directory/$F" && touch -r "$F" "/new/directory/$F"
     done

That will compare all the files and adjust the timestamp on the files
that are still the same as those on the backup.  Note that, despite
the quoting, the script will break if any of the filenames have embedded
whitespace because the "for F in *" will be parsed incorrectly.

-- 
Bob Nichols     "NOSPAM" is really part of my email address.
                 Do NOT delete it.