mac bash 上文件的格式化修改日期/时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12169710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
formatted modified date/time of file on mac bash?
提问by David Burson
In my bash script on mac (snow leopard) I have a path and filename, and I need to get the modified date/time of that file. I found I could do:stat -f "%m" $MYFILE
在 mac(雪豹)上的 bash 脚本中,我有一个路径和文件名,我需要获取该文件的修改日期/时间。我发现我可以这样做:stat -f "%m" $MYFILE
However, that returns what I assume is epoch date/time. I need the date/time formatted: YYYYMMDDThhmmss. I've found all kinds of options (like date) that apparently depend on GNU, which on my mac I don't have.
但是,这会返回我假设的纪元日期/时间。我需要格式化的日期/时间:YYYYMMDDThhmmss. 我发现date了显然依赖于 GNU 的各种选项(例如),而我的 Mac 上没有。
What's the standard way to get a file's date/time modified in a user-specified format on mac (BSD?) bash. Or at least, a date/time formatting function that I can pass the result of my statcall above to.
在 mac (BSD?) bash 上以用户指定的格式修改文件日期/时间的标准方法是什么。或者至少是一个日期/时间格式化函数,我可以将stat上面调用的结果传递给它。
回答by chepner
It's actually pretty simple, but different enough from GNU datethat it's nowhere near obvious:
它实际上非常简单,但与 GNU 足够不同,date以至于它远不明显:
date -r $TIMESTAMP +%Y%m%dT%H%M%S
To get statto do the formatting:
要stat进行格式化:
stat -f "%Sm" -t "%Y%m%dT%H%M%S" FILE
回答by Yordan Georgiev
# 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

