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!

  • What do you mean by "creation date"? Is it the "Birth" date as shown by stat? Commented May 7, 2019 at 13:42
  • Yes the Birth date.
    – emileber
    Commented May 7, 2019 at 13:54
  • or the date set by setFile -d I'm assuming they're the same?
    – emileber
    Commented May 7, 2019 at 13:59

2 Answers 2


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, ...

Not an explanation but a demonstration of "sh as $0"

Say you have a script like this, named script.sh

echo "$0"
echo "$1"
echo "$2"

Running it with some parameters gives:

$ sh script.sh first second third

However, when you use -c "$script_body", then the first argument slides into $0

$ sh -c 'echo "$0"; echo "$1"; echo "$2"' first second third

This is important because the special variables "$*" and "$@" takes the positional arguments starting with $1

  • Can anyone explain 'The 2nd "sh" will become the $0 inside the spawned shell' please? I understand that the filenames all populate f from $@, but where does the sh as $0 come in? Thanks
    – Chris
    Commented Mar 4 at 14:43
  • @Chris, is that helpful? Commented Mar 4 at 15:17
  • Very helpful indeed, thank you! I also needed this excellent answer to understand that sh could be any string, not necessarily the sh command, and find normally passes a filename at a time which is what + changes to many filenames in one call, and the {} is where find interpolates the filename/s. The second sh bit caused some head scratching!
    – Chris
    Commented Mar 7 at 14:29
  • For anyone else, someone just told me that the string sh is used in error reporting, so the reason the second sh is always sh, even though it is just to use up $0, is because sh tells the reader that it is the sh -c command that generated the error message.
    – Chris
    Commented Mar 8 at 11:07

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")";


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.

  • 1
    I think you want your stat statement like this-> stat -f'%Sm' -t "%m/%d/%Y %H:%M:%S" FILE.
    – fd0
    Commented May 7, 2019 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .