UNIX stat time format

旧时模样 提交于 2019-12-12 07:48:46

问题


Is it possible to format the time output of stat? I am using

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?

An example output follows:

/lib drwxr-xr-x 2010-11-15 04:02:38.000000000 -0800

回答1:


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:

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')"



回答2:


You can simply strip of the decimal portion like this:

stat -c '%n %A %z' "$filename" | sed 's/\(:[0-9]\{2\}\)\.[0-9]* /\1 /'

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



来源:https://stackoverflow.com/questions/4181552/unix-stat-time-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!