Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Background and issue

I moved to Mac OS X recently and bumped into the "feature" of Mac where copying files from an external drive resets the file modification/update date/timestamp to the current date (which Windows does not), causing a disaster for my 10+ years of backup work files where date is important.

So, before I learned how to avoid that (e.g. using the -p "preserve" flag in the "cp" copy command) I have in the meantime added to my new Mac hard drive many more files as well as updating existing old files.

Current state

  • I have a backup external hard drive with all my old data and proper modification dates
  • I have a Mac hard drive with reset modification file dates (a single or two particular days)
  • The Mac hard drive has all the "true" and "current" file contents with files modified and added

What I need

I need to

  • Copy all the original files from the external harddrive, preserving file metadata (really only modified date), but ONLY overriding the new internal Mac hard drive IF
    • The file contents (md5 or whatever) is the same OR
    • The file was updated after the day (which of course I can see on all files) on which the original disasterous cope was performed (implying the file is new or modified)
  • Ensure the copy leaves all the new and modified files completely intact on the Mac internal hard drive.
  • "No prompting/stopping of the copy of any kind (ie not verbose) is required but is o.k."
  • "Recursive copy - obviously I would like to copy all* files folders and subfolders found in export"

How can this be done, any idea?

I've read all I could on Stack Overflow and checked man pages and info on both "rsync" and "cp" but cannot find a way...

Does anyone have any good tools, scripts, tricks, ideas?

share|improve this question

migrated from stackoverflow.com Apr 25 '11 at 8:26

This question came from our site for professional and enthusiast programmers.

    
Copying files from external drives in Finder does not change their modification date. –  Daniel Beck Apr 25 '11 at 8:31
    
Copy everything off the drive again, keeping the attributes intact. Make a folder comparison using DiffMerge or FileMerge or any of the other folder hierarchy comparison utilities of the potentially modified files and the external drive originals, them copy the modified ones over your newer "archived" copy manually. If they aren't that numerous, it's the quickest solution. –  Daniel Beck Apr 25 '11 at 10:24
    
I looked at those tools (had them both, used DiffMerge for code comparison in the past), they don't seem to be able to cut it in any reasonably easy way.. Also, there are actually 100s of thousands of files (docs, notes, mails, code snippets etc) so its a bit of a hairy task.. If anyone has any other suggestions it would be appreciated.. Just looked at the Java API and I can actually change file modification dates using that so maybe that is the solution but I suppose it would take some time to write and test.. Any other ideas? –  Daniel Iversen Apr 25 '11 at 10:40
    
I recently used a rather large find expression on a linux system to find differences between two file systems. I had find print full file paths and attributes (if they were not folders, their sizes are pretty much useless), as well as exec one of the checksum programs (md5sum I think). I piped the output into files and diffed those. You could do that, then use the files determined this way as input for any copying script (either as inclusion or exclusion). –  Daniel Beck Apr 25 '11 at 11:29
    
I think rsync can do that. –  Keith Apr 26 '11 at 5:29
add comment

1 Answer

up vote 1 down vote accepted

Ended up writing a java program to do this... Won't post the code here because its quick, dirty and spaghetti ;) ... It is robust however and you can download the simple source code from www.nexle.dk/permanent-downloads/StackExchange/FileModChanger.java.zip to use modify, compile and use freely (but no warranty, check and test before you run).

What the tool does essentially is that it takes the last modified dates of files in one folder structure and applies it to files in another folder provided THAT;

  • the file names and paths are the same (security)
  • the file contents is the same (md5 check, again for safety)
  • the date is not already the same for both files

I ran it against ~500,000 files and it seemed to work o.k.

Some notes;

  • it only changes last modified date/time/timestamp so it should be safe
  • the tool is default in "dry run" mode meaning it will show you what it would do, but not perform changes ('real' mode is activated by a command line argument 'real)
  • It uses some code from the web (credit inside source file).
  • It could be faster but its fast enough
  • It has a cute little progress indicator while parsing through all the directories for files the first time ;)

Some code you might be able to draw inspiration from as well if you down want to trawl through the whole code;

How to change file modification date in Java;

    File sourceFile = new File("/tmp/sourcefile");      
    File targetFile = new File("/tmp/targetfile");

    long sourceDateModified = sourceFile.lastModified();

    targetFile.setLastModified(sourceDateModified);

Hopefully someone else might be able to find it useful as well. Thanks for all the comments and hints though.


Usage and console output for the finished tool you can download (see above);

$ java -classpath . FileModChanger 

USAGE (dry run, NO file changes made):
  FileModChanger <source folder with  original dates> <target folder with new/erased dates>

USAGE (real run,file date changes WILL be made):
  FileModChanger <source folder with  original dates> <target folder with new/erased dates> real





$ java -classpath . FileModChanger /Volumes/Untitled/D/knowledge/ISYS\ Desktop\ 6/ /Knowledge/ISYS\ Desktop\ 6/
Starting DRY run....
Starting DRY run....
Starting DRY run....
Starting to retrieve list of files (this may take a LONG time)...
0 folders processed 
DONE creating the list of files, starting comparison...



About to operate on Volumes/Untitled/D/knowledge/IS[...]S Desktop 6/ISYSDesktop6Setup.exe:
 SRC:Tue Jul 01 11:29:46 EST 2003 - bff684bfac4ed37e4a6b9a05ab625855 - Volumes/Untitled/D/knowledge/IS[...]S Desktop 6/ISYSDesktop6Setup.exe
 TRG:Tue Dec 21 04:05:24 EST 2010 - bff684bfac4ed37e4a6b9a05ab625855 - /Knowledge/ISYS Desktop 6/ISYSDesktop6Setup.exe


DONE processing all files..










$ java -classpath . FileModChanger /Volumes/Untitled/D/knowledge/ISYS\ Desktop\ 6/ /Knowledge/ISYS\ Desktop\ 6/ real
Starting REAL run....
Starting REAL run....
Starting REAL run....
Starting to retrieve list of files (this may take a LONG time)...
0 folders processed 
DONE creating the list of files, starting comparison...



About to operate on Volumes/Untitled/D/knowledge/IS[...]S Desktop 6/ISYSDesktop6Setup.exe:
 SRC:Tue Jul 01 11:29:46 EST 2003 - bff684bfac4ed37e4a6b9a05ab625855 - Volumes/Untitled/D/knowledge/IS[...]S Desktop 6/ISYSDesktop6Setup.exe
 TRG:Tue Dec 21 04:05:24 EST 2010 - bff684bfac4ed37e4a6b9a05ab625855 - /Knowledge/ISYS Desktop 6/ISYSDesktop6Setup.exe
 RESULT:
   - BEFORE:    Tue Dec 21 04:05:24 EST 2010
   - AFTER:     Tue Jul 01 11:29:46 EST 2003
DONE processing all files..

also, you may need to increase Java's memory if you have lots of files and folders.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.