If you have both SetFile
and GetFileInfo
, you can set the creation date to that of the modified date on .IMG
files by doing the following:
In Terminal, first change directory cd
to the location of the target .IMG
files.
cd /path/to/target/files
Then execute the following compound command:
for f in *.[iI][mM][gG]; do m="$(GetFileInfo -m "$f")"; SetFile -m "$m" -d "$m" "$f"; done
Note: It is always a good idea to first test on a small sample of the target files copied to another folder and check the results first, then proceed on the original files. One should also always have normal regular backups as well before proceeding, e.g using Time Machine or other backup software.
If you do not have GetFileInfo
and want to use stat
, then directly after do
replace:
m="$(GetFileInfo -m "$f")";
With:
m="$(stat -f'%Sm' -t "%m/%d/%Y %H:%M:%S" "$f")";
Giving you the full compound command:
for f in *.[iI][mM][gG]; do m="$(stat -f'%Sm' -t "%m/%d/%Y %H:%M:%S" "$f")"; SetFile -m "$m" -d "$m" "$f"; done
Note: This assumes you do have at least one .IMG
file in the directory, otherwise it causes a non-fatal error, meaning nothing has changed:
stat: *.[iI][mM][gG]: stat: No such file or directory
ERROR: invalid date/time
To use find
, see glenn jackman's answer.