'Date Created' -> 'Date Modified'
August 6, 2007 9:48 PM   Subscribe

I have Mac OS X, with its Unix underpinnings. I have a number of text files through which I did a search-and-replace. Unfortunately, that altered all of the timestamps on the files. I'm trying to restore things via the "Date Created" info.

But my Google-fu is experiencing massive failure, perhaps due to the wrong terms. Can this be done via "touch", or is the "date created" info not accessible via the shell script? Is it AppleScriptable?

I'm sure someone's already had to do this and put it up on the Web somewhere — I just can't find the puppy.

(Also, hoping that the solution is one that can be applied to multiple files in a batch situation.)

Thanks, everyone.
posted by WCityMike to Computers & Internet (12 answers total) 2 users marked this as a favorite
 
$ man touch

You want -t on OS X, -d on linux.

$ for i in * ; do touch -t [timestring] $i;done

posted by b1tr0t at 10:03 PM on August 6, 2007


And 'stat' ought to be able to tell you the create time, which you can then pass to 'touch' to set the 'last modified' time, or whatever it is you want.
posted by autojack at 10:24 PM on August 6, 2007


on the applescript end: creation date is read-only vs. Finder. but something like akua sweets osaxen will let you do it via apply catalog info ....
posted by dorian at 10:28 PM on August 6, 2007


You clobbered the mtime. You may be interested in using the ctime to update it.

Beware that "ctime" doesn't mean "create time", but rather "change time". That refers to the metainfo about the file, like its name or permissions or ownership, and so on. It is set to the time of creation when the file is created, but any metainfo change after that changes ctime also. Make sure that's good enough for you before you make any assumptions.

HFS+ may have some special attribute that stores the creation time, but beware that the usual Unixish "ctime" ain't it.
posted by cmiller at 3:01 AM on August 7, 2007


Listen, I appreciate the answers thus far, but I really need much more handholding ... something along the lines of "yo, dummy, do x, y, and z." I have no idea how to apply ctime or stat to this situation.

Much obliged in advance!
posted by WCityMike at 5:47 AM on August 7, 2007


Open Terminal.app. Type/paste:
cd /path/to/files
for i in *; do
  MOD=`stat -f %SB -t %Y%m%d%H%M.%S "$i"`
  echo $i -- $MOD
# touch -t $MOD "$i"
done

If the times look sensible, uncomment the touch -t line (remove the #)

Change the stat format argument %SB to %Sc if OS X doesn't have an "inode birth" time; I've only tested this in FreeBSD.
posted by Freaky at 7:18 AM on August 7, 2007


Note this will change all the files in there; if you want to change just a specific set of files, you can do:

for i in foo bar "file with space" wibble; do
posted by Freaky at 7:19 AM on August 7, 2007


Oh, you probably want %Sc anyway, since inode birth time means when the inode was first allocated, not when it was allocated and a file created there. It may still be of use if the ctime has been blatted; depends if the inode has been reused or not, and whether OS X uses the same semantics.
posted by Freaky at 7:43 AM on August 7, 2007


This didn't seem to work. %SB kept being bumped as a bad format, and %Sc only yielded date-modified results.

Would 'mdls' or 'getinfo' (parsed with 'grep') yield the desired results? I'm just not sure how to translate the info these produce into the format touch would be looking for.
posted by WCityMike at 5:46 PM on August 7, 2007


Looks like this'd work:

MOD=`/usr/bin/mdls "$i" | /usr/bin/awk -F" " '/kMDItemFSCreationDate/{print $3$4}' | /usr/bin/sed 's/-//g' | /usr/bin/sed 's/://' | /usr/bin/sed 's/:/./'`
posted by WCityMike at 6:23 PM on August 7, 2007


Ah, Darwin is weird ;)

With a touch more awk-fu:

mdls -name kMDItemFSCreationDate "$i" |tail -1 | awk -F'=|\\+' '{gsub(/[-: ]/,"");print $2}' |sed -e 's/\([0-9]\{2\}\)$/.\1/'

Note you can chain expressions in sed, so you could also write:

sed -e 's/-//g' -e 's/://' -e 's/:/./'

Instead of running sed multiple times. If accuracy's important you might need to translate from localtime to UTC; at this point I'd probably just dip into Ruby:

mdls -name kMDItemFSCreationDate "$i" |tail -1|ruby -rtime -e 't = Time.parse(STDIN.gets.split(" = ")[1].chomp).gmtime;File.utime(t,t,*ARGV)' "$i"

This also resets access time.
posted by Freaky at 4:18 AM on August 8, 2007


Thanks. :)
posted by WCityMike at 5:53 AM on August 8, 2007


« Older USB for PCI-X for my PowerMac G5   |   Is this formula an underestimate of how many... Newer »
This thread is closed to new comments.