GNU date(1)
understands the %N
format spec, which outputs nanoseconds, so:
$ date +%H:%m:%S.%N
outputs 19:10:03.725196000
BSD date doesn't understand %N. How can I print the current time with sub-second precision on OS X?
command line
GNU date(1)
understands the %N
format spec, which outputs nanoseconds, so:
$ date +%H:%m:%S.%N
outputs 19:10:03.725196000
BSD date doesn't understand %N. How can I print the current time with sub-second precision on OS X?
Best Answer
As you said, BSD
date
doesn't support%N
. So the straightforward solution is to download GNU'sdate
and install it to either~/bin
or/usr/local/bin
and call that when you need to use%N
.However, I would not advise replacing the version that ships with your Mac with the new one. Most of these coreutil programs are written such that their output can be understood by other programs, not necessarily humans, so reliability in output is a necessity. There might be several scripts in your mac that parse the output of BSD's
date
and any possible changes introduced by replacing with GNU's might or might not break things.The simplest way to do this safely on a Mac is to use
homebrew
and install coreutils.The GNU equivalent will be named
gdate
and is located in/usr/local/Cellar/coreutils/8.16/bin/
. You can also set an alias in your.bashrc
or its equivalent as