find -printf on Mac (-printf unknown option)

I have a little bit of code that I use to get an MD5 sum of the most recently modified file time

find ./media -type f -printf "%TY-%Tm-%Td %TT %p \n" | sort | more | tail -1 | md5 -r | awk '{print $1}'

I use it for generating unique keys for my CDN files, the logic behind this is if a file changes the key changes so the files are un-cached and reloaded.

Anyway, when I try to run that on my Mac I get the error '-printf unknown option'
I looked through the man page but couldn't find anything similar, how do I get this too work?


Solution 1:

printf is specific to GNU find and not available on OS X's find by default.

Install findutils on your Mac, e.g. using Homebrew using the following command:

brew install findutils

You can alternatively use Macports or Fink, they likely also have this package available.

To actually use gnu find you will need to use gfind since find will still link to OSX's find located in /usr/bin/find.

Homebrew comes with a tap for cases where system commands are duplicated by the commands installed via homebrew.
Simply do brew tap homebrew/dupes, and then for above example brew install findutils.
Now you should be able to use homebrews find command without having to explicitly call it via gfind.

Solution 2:

You could use the -ls flag instead to get the same information (and then some), which might work just as well for your purposes.