Mac で GNU 版の date コマンドを使う方法


  • brew install coreutils して gdate で実行すれば OK

モチベーション

  • Mac にデフォルトでインストールされている date コマンドは BSD 版なので、ちょくちょく異なるオプションがありストレス

GNU 版 date コマンドを Mac 環境で使う方法

  • CoreUtils を homebrew でインストールすれば良い
  • 既存の date コマンドを区別するため gdate で実行することが出来る

    brew install coreutils

coreutils — Homebrew Formulae
https://formulae.brew.sh/formula/coreutils#default


which してみる

% which date
/bin/date
% which gdate
/usr/local/bin/gdate

環境

% sw_vers
ProductName:	macOS
ProductVersion:	11.6.2
BuildVersion:	20G314

% brew --version
Homebrew 3.3.9
Homebrew/homebrew-core (git revision a3f1088c7f1; last commit 2021-12-23)
Homebrew/homebrew-cask (git revision 8e30a4b62e; last commit 2021-12-23)

% brew info coreutils
coreutils: stable 9.0 (bottled), HEAD
GNU File, Shell, and Text utilities
https://www.gnu.org/software/coreutils
Conflicts with:
  aardvark_shell_utils (because both install `realpath` binaries)
  b2sum (because both install `b2sum` binaries)
  ganglia (because both install `gstat` binaries)
  gdu (because both install `gdu` binaries)
  gegl (because both install `gcut` binaries)
  idutils (because both install `gid` and `gid.1`)
  md5sha1sum (because both install `md5sum` and `sha1sum` binaries)
  truncate (because both install `truncate` binaries)
  uutils-coreutils (because coreutils and uutils-coreutils install the same binaries)
/usr/local/Cellar/coreutils/9.0 (480 files, 12.7MB) *
  Poured from bottle on 2021-12-23 at 19:01:00
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/coreutils.rb
License: GPL-3.0-or-later
==> Dependencies
Required: gmp ✔
==> Options
--HEAD
	Install HEAD version
==> Caveats
Commands also provided by macOS and the commands dir, dircolors, vdir have been installed with the prefix "g".
If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH with:
  PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
==> Analytics
install: 79,559 (30 days), 310,828 (90 days), 870,614 (365 days)
install-on-request: 54,568 (30 days), 220,595 (90 days), 637,163 (365 days)
build-error: 90 (30 days)

% gdate --version
date (GNU coreutils) 9.0
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.