在macbash上格式化修改的文件日期/时间



在我的mac(雪豹)上的bash脚本中,我有一个路径和文件名,我需要获得该文件的修改日期/时间。我发现我可以:
stat -f "%m" $MYFILE

然而,它返回了我认为的epoch日期/时间。我需要日期/时间格式:YYYYMMDDThhmmss。我发现了各种明显依赖GNU的选项(比如date),而在我的mac上我没有。

在mac(BSD?)bash上,以用户指定的格式修改文件的日期/时间的标准方法是什么。或者至少是一个日期/时间格式化函数,我可以将上面stat调用的结果传递给.

它实际上很简单,但与GNU date的不同之处在于它并不明显:

date -r $TIMESTAMP +%Y%m%dT%H%M%S

stat进行格式化:

stat -f "%Sm" -t "%Y%m%dT%H%M%S" FILE
# how-to list all the files and dir in a dir sorted by their
# modified time in the different shells 
# usually mac os / Unix / FreeBSD stat 
stat -f "%Sm %N" -t "%Y-%m-%d %H:%M:%S" ~/opt/comp/proj/*|sort 
# STDOUT output:
# 2018-03-27 15:41:13 ~/opt/comp/proj/foo
# 2018-03-28 14:04:11 ~/opt/comp/proj/bar
# GNU Utils ( usually on Linux ) stat 
# STDOUT output:
stat -c "%y %n" ~/opt/comp/proj/*|sort
# 2018-03-29 09:15:18.297435000 +0300 ~/opt/comp/proj/bar
# 2018-03-29 09:15:18.297435000 +0300 ~/opt/comp/proj/foo

最新更新