bash UNIX 统计时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4181552/
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
UNIX stat time format
提问by scott77777
Is it possible to format the time output of stat? I am using
是否可以格式化stat的时间输出?我在用
stat -c '%n %A %z' $filename
in a bash script, but its time format is not what I want. Is it possible to change this format in the command, or would I have to manually do it later?
在 bash 脚本中,但它的时间格式不是我想要的。是否可以在命令中更改此格式,还是必须稍后手动更改?
An example output follows:
示例输出如下:
/lib drwxr-xr-x 2010-11-15 04:02:38.000000000 -0800
采纳答案by Adrian Pronk
You could try something like:
你可以尝试这样的事情:
date -d "1970-01-01 + $(stat -c '%Z' $filename ) secs"
Which gives you only the date. You can format the date using date's formatting options (see man date), for example:
它只给你日期。您可以使用日期的格式化选项来格式化日期(请参阅man date),例如:
date -d "1970-01-01 + $(stat -c '%Z' $filename ) secs" '+%F %X'
This doesn't give you the name and permissions but you may be able to do that like:
这不会为您提供名称和权限,但您可以这样做:
echo "$(stat -c '%n %A' $filename) $(date -d "1970-01-01 + $(stat -c '%Z' $filename ) secs" '+%F %X')"
回答by Paused until further notice.
You can simply strip of the decimal portion like this:
您可以像这样简单地去除小数部分:
stat -c '%n %A %z' "$filename" | sed 's/\(:[0-9]\{2\}\)\.[0-9]* / /'
Edit:
编辑:
Here's another way to truncate the decimal portion:
这是截断小数部分的另一种方法:
stat -c '%n %A %.19z' "$filename"
This depends on the date being 19 characters long: 2010-11-15 04:02:38
这取决于 19 个字符长的日期: 2010-11-15 04:02:38

