Change file creation date to modification date using terminal
So I have a bunch of pictures that have incorrect creation dates but their real creation dates are the same as the current modification dates. Is it possible to correct this using some batch command in terminal? I've been messing around with the setFile + stat commands but it's not really working out since I'm terrible with terminal. Does anyone here now the correct way to do this?
Also is it possible to incorporate this into a find command? So that all pics with "IMG" in them get this treatment but no others?
Thanks in advance!
To use @user3439894's answer with find
, use the -exec command {} +
clause, and put the for loop into a sh
script:
find some/path -iname \*.img -exec sh -c 'for f in "$@"; do m="$(GetFileInfo -m "$f")"; SetFile -m "$m" -d "$m" "$f"; done' sh {} +
The 2nd "sh" will become the $0 inside the spawned shell, and the .img filenames will be the positional parameters $1, $2, ...
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.