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. The source files are just an old copy on another drive that I found when cleaning up things...
Robert Moskowitz spake the following on 8/6/2007 2:40 PM:> 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. > > The source files are just an old copy on another drive that I found when > cleaning up things...Can you restore the backups, and then cp -u from the existing directory over the restored copy? -- MailScanner is like deodorant... You hope everybody uses it, and you notice quickly if they don't!!!!
Scott Silva wrote:> Robert Moskowitz spake the following on 8/6/2007 2:40 PM: > >> 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. >> >> The source files are just an old copy on another drive that I found when >> cleaning up things... >> > Can you restore the backups, and then cp -u from the existing directory over > the restored copy?No. Because all the files, changed or not since that date, are newer than what is on the backup. So it would overwrite everything.
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.
On Mon, 2007-08-06 at 17:40 -0400, 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 suggest "man find" and focus on the (new to me) versions of "newer". I think, IIUC, that one of those tests will meet your needs. When the test passes, an appropriate "exec ..." may do what you want.> > The source files are just an old copy on another drive that I found when > cleaning up things... > <snip>HTH -- Bill
On Mon, 2007-08-06 at 17:40 -0400, Robert Moskowitz wrote:> <snip>P.S. I make (the possibly invalid assumption) that the original directory timestamp is a valid comparator to use in seeing if the file(s) have been modified. For you case, if this is valid, the negation of the test might be needed. -- Bill
CentOS mailing list <centos at centos.org> writes:>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. > >The source files are just an old copy on another drive that I found >when >cleaning up things... >SRC=/my/restored/files DST=/the/working/tree export SRC DST find "$SRC" -type f -print | while read A; do [ "${A}" -ot "${DST}${A##$SRC}" ] && /bin/touch -r ${A} "${DST}${A##$SRC}"; done Note that using this syntax, the two variables cannot be set in-place on the same command line as "find ..."; it only seems to work for me if they're exported as environment variables. A more expanded version of the same, with commentary: cat > fix_timestamps.sh << __EOF__ #!/bin/sh ### Scans a source directory for files, setting the dates of same-named files in a target directory ### Adam Thompson <athompson at sjsd.net> 2007-Aug-07 # Source directory - no need for trailing slash SRC=/my/restored/files # Target directory - no need for trailing slash DST=/the/working/tree # Locate all _files_ only find "$SRC" -type f -print | ( # subshell not necessary, only here for readability # read each line of input from the pipe into $A while read A; do # if's can be shortened to && most times # Proceed only if source file is OlderThan target file if [ "${A}" -ot "${DST}${A##$SRC}" ]; then # Proceed only if target file exists # Probably being paranoid, since -ot should have already failed... if [ -f "${DST}${A##$SRC}" ]; then # Use "touch" to reset the timestamp on the file \ # instead of re-copying the data # "-r" == "--reference", uses the source file's \ # timestamp to set the destination file's timestamp /bin/touch -r ${A} "${DST}${A##$SRC}" fi fi done ) __EOF__ Obviously the one-liner above is going to be marginally faster, especially on slower CPUs, but the difference should be minimal - bash isn't that much of a CPU hog. -Adam Thompson Divisional IT Department, St. James-Assiniboia School Division 150 Moray St., Winnipeg, MB, R3J 3A2 athompson at sjsd.net / tel: (204) 837-5886 x222 / fax: (204) 885-3178 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.centos.org/pipermail/centos/attachments/20070808/47a03b6d/attachment-0001.html>